Noob question regarding spawning enemies (2D shooter)

Hi guys,

I’m a total noob with Unity and I have struck an issue with my game… I’m not quite sure how to spawn enemies.

I was thinking I’d wrap a WaitForSeconds inside a good old for loop, but a “yield” doesn’t seem to work within a function update…

I’ve looked all through the scripting reference but can’t seem to work out how to get it going.

Basically, I want something like this:

for (int i =0; i < 10; ++i) // Counts up the number of enemies to spawn.
{
// Spawn Enemy
// Wait a certain amount of time
}

I know how to spawn objects… just stumped on the waiting bit…

Any help greatly appreciated :slight_smile:

Cheers!

You cannot yield in Update. Make a function and call it from within update.

function Update ()
{
    SpawnEnemy ();
}

function SpawnEnemy ()
{
    for (int i =0; i < 10; ++i) // Counts up the number of enemies to spawn.
    {
        // Spawn Enemy
        yield WaitForSeconds (yourTime);
    }
}

Ah, thanks!!! Simple… silly me :wink:

Appreciate the quick resonse!

Another quick question… what would be the best way to spawn the enemies? I was thinking about having empty game objects with the spawn scripts attached… is this usually how it’s done?