Instantiate a certain amount of prefabs

Hello their Unity developers, I am currently making a T.D/F.P.S like game where waves of enemies come at you and you have to defend yourself from them, I have a code that spawns in the enemies at a place but the problem is that it won’t stop Instantiating it. I know this is because of the code being called in Update() but I want to check the level you are on. Heres my code:

  public function SpawnEnemies(enemies : int, spaceBetweenEnemies : float, EnemyForWave : Transform, SpawnPoint : Transform) : int
    {
    
    
    for(var i = 0; i < enemies; i++)
     {
     
     /*if(enemies <= enemies) this line does not work, i've also tried if(enemies >= enemies)
     {*/
     Instantiate(EnemyForWave, SpawnPoint.position, SpawnPoint.rotation);
     }
     
     //}
    
     
    }
    
    
    function Update()
    {
    
    //the problem is here
    
    if(wave == 1)
    {
    SpawnEnemies(5, 1f, RegAi, spawnArea);
    }
    
     }

Please help

Sorry if I maybe wasn’t clear.

Thanks :slight_smile:

_1

What’s happening here is that SpawnEnemies is being called every frame when the round is 1, and it’s spawning 5 enemies.

I’m too tired to write up an elegant fix, so a bool trigger will suffice :P.

You need to add this somewhere:

 var trigger : bool = true; 

 public function SpawnEnemies(enemies : int, spaceBetweenEnemies : float, 
                        EnemyForWave : Transform, SpawnPoint : Transform) : int
 {

       trigger = false; //This stops the function being called in the next frame.

       for(var i = 0; i < enemies; i++)
       {
             Instantiate(EnemyForWave, SpawnPoint.position, SpawnPoint.rotation);
       }
  }

  function Update()
  {
       if(trigger == true) //The bool we just made
       {
           if(wave == 1)
            {
                SpawnEnemies(5, 1f, RegAi, spawnArea);
            }
       }
  }

So yeah , to reiterate what’s happening in your code, every frame update is called it’s instantiating 5 gameObjects.

With this new code - the first frame trigger == true so SpawnEnemies is called, then at the start of SpawnEnemies we set the trigger to false, so the enemies are instantiated and on the next frame trigger == false, so the function (SpawnEnemies) isn’t called.

At the end of the round or someplace you’ll want to set the trigger to true so that new enemies are spawned.

As stated in a comment below your answer, this should be placed in the start function really. Just posted a fix to the code that you actually posted.

If you did it in the start function you could replace 5 (the literal you use as the argument value ‘enemies’ with a variable and multiply that by the level, to create more enemies each level.

var numberOfEnemies : int = 5; // Base amount of enemies;

function Start()
{
     var numberOfEnemiesInWave = numberOfEnemies * wave; // 1st wave 5 enemies
                                                         // 2nd wave 10 enemies
                            //Play around with the values, i'm sure you grasp
                            //the concept..

     SpawnEnemies(numberOfEnemiesInWave, 1f, RegAi, spawnArea);
}

Something like that :stuck_out_tongue: