im trying to create a mario kart style crate that spawns in the middle of the race track.
- at each spawn point i want to choose between 4 prefabs and spawn it.
- once its been hit wait 3 seconds to respawn another random box.
this is the script i have
var BulletCratePrefab : Transform;
var ExploBulletCratePrefab : Transform;
var HitPointCratePrefab : Transform;
var NosCratePrefab : Transform;
var CrateInstance = gameObject;
private var spawnPoint : Vector3;
function Update()
{
while(true)
{
Spawn();
while(CrateInstance) //wait until the current crate on spawn point is dead.
{
return null;
}
}
}
function Spawn ()
{
var index = Random.Range(0 , 3);
spawnPoint = this.transform.position;
// choose which prefab to put on track
if(index == 0)
{
CrateInstance = (gameObject)Instantiate (BulletCratePrefab, spawnPoint, this.transform.rotation);
}
else if(index == 1)
{
CrateInstance = (gameObject)Instantiate (ExploBulletCratePrefab, spawnPoint, this.transform.rotation);
}
else if(index == 2)
{
CrateInstance = (gameObject)Instantiate (HitPointCratePrefab, spawnPoint, this.transform.rotation);
}
else if(index == 3)
{
CrateInstance = (gameObject)Instantiate (NosCratePrefab, spawnPoint, this.transform.rotation);
}
}
the only referrence i had was for c# so im not sure if i can do it the same.
Edit: this is the c# script i have. it selects 1 of xAmount and spawns a target, once its dead it spawns again.
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public GameObject TargetPrefab; // Set this in inspector to the target prefab.
public int RandTimeMin = 3; // Minimum amount of time before spawning enemy.
public int RandTimeMax = 10; // Maximum amount of time before spawning enemy.
GameObject targetInstance;
Transform[] spawnpoints;
public int SpawnNumber = 10;
int Spawned = 0;
int lastIndex =0;
IEnumerator Start()
{
spawnpoints = GetComponentsInChildren<Transform>();
while (true && Spawned < SpawnNumber)
{
yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
Spawn();
while (targetInstance) // Wait for target to die.
{
yield return null;
}
yield return new WaitForSeconds(Random.Range(RandTimeMin, RandTimeMax)); // wait X for next target.
}
yield return new WaitForSeconds(6);
Application.LoadLevel(0);
}
void Spawn()
{
int index = 0;
Spawned++;
while(lastIndex == index )
{
index = Random.Range(0, spawnpoints.Length - 1);
}
var spawnpoint = spawnpoints[index];
targetInstance = (GameObject)Instantiate(TargetPrefab, spawnpoint.position, spawnpoint.rotation);
}
}
instead of creating a list of spawnpoints, i thought it would be easier to handle each spawnpoint individually so if its crate is destroyed it automatically deals with it. as you can see i havent translated the c# in to Js and my idea very well.
gameObject CrateInstance; should be var CrateInstance : gameObject;
– anon73128419thanks i couldnt remember how to do it lol
– anon54345742