x


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!

more ▼

asked Jan 31 '11 at 10:31 PM

NDJ23 gravatar image

NDJ23
32 6 6 12

You didn't highlight enough code before you hit the 101010 button.

Feb 01 '11 at 12:16 AM Jessy

How are you killing an enemy?

Feb 01 '11 at 12:21 AM tertle
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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;

}
more ▼

answered Feb 01 '11 at 12:32 AM

reissgrant gravatar image

reissgrant
246 14 15 25

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!

Feb 01 '11 at 03:19 AM NDJ23
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1358
x958
x294
x36

asked: Jan 31 '11 at 10:31 PM

Seen: 1643 times

Last Updated: Feb 01 '11 at 12:16 AM