- Home /
i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies = 20; // Total number of enemies to spawn.
// Don't exceed this amount of time between spawning enemies randomly.
function Start(){
}
function Update(){
if (Input.GetButtonDown("Fire2")) {
Spawn();
enemyPrefabs= GameObject.FindGameObjectsWithTag("Clone");
}
}
function Spawn(){
for (i=0; i<amountEnemies; i++){ // How many enemies to instantiate total
// How long to wait before another enemy is instantiated.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
Instantiate(obj, pos.position, pos.rotation);
}
}
Comment
Answer by d2 · Mar 06, 2015 at 05:54 PM
put the GameObject.FindGameObjectsWithTag("Clone"); before the Spawn() call, and add a validation:
if(enemyPrefabs.lenght > 0)
Spawn();
is goood , is funtion but write error: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)