- Home /
Cycle Through Array of Enemies
I need some help with a script I am trying to write.
I'd like to cycle through this array of gameobjects, activating the next enemy in the array every X seconds.
The problem is this:
-for loop spawns x number of enemies after x seconds.
-player kills enemy[1]
-for loop spawns enemy[1], instead of the next enemy in the array
How do I make the loop continue through the objects, before starting back at the beginning?
var Enemies : GameObject[]; var totalEnemies : int; totalEnemies = 10; var Player : Transform;
function Spawner() {
for(var i=0;i < totalEnemies; i++)
{
yield WaitForSeconds(5);
Enemies[i].active = true;
Enemies[i].transform.position = Player.position;
}
}
Thanks for any input!
You didn't highlight enough code before you hit the 101010 button.
Answer by reissgrant · Feb 01, 2011 at 12:32 AM
Try this instead:
var Enemies : GameObject[]; var totalEnemies : int = 10; var Player : Transform; var currentEnemy : int = 0; var secondsPassed : float = 0;
function Update() {
if(currentEnemy == totalEnemies){
return;
} else {
if(secondsPassed > 5){
Enemies[currentEnemy].active = true;
Enemies[currentEnemy].transform.position = Player.position;
secondsPassed = 0;
currentEnemy += 1;
}
}
secondsPassed += Time.deltaTime;
}
Thanks for the help reissgrant. That works perfectly, I had started experimenting with using Update as the actual loop mechanism, but couldn't figure out the counter.
Thanks again!
Your answer
Follow this Question
Related Questions
Make a Button For Every String in an Array of Level Names 1 Answer
Loop through array until certain value is found. 2 Answers
Trouble with for loop out of range. Simple? Maybe. 1 Answer
Trouble checking against array objects 0 Answers
Can someone give me an example on looping through arrays? 0 Answers