Spawning enemies if there is no other enemy left.

Hello everyone. In my game, player needs to defend a point. I spawn 6 enemies at their spawn points per 7 seconds with this code :

function Start () {

while(true) {
 yield new WaitForSeconds(7);
Instantiate(s1,spawn1.position,Quaternion.identity);
Instantiate(s2,spawn2.position,Quaternion.identity);
Instantiate(s3,spawn3.position,Quaternion.identity);
Instantiate(s4,spawn4.position,Quaternion.identity);
Instantiate(s5,spawn5.position,Quaternion.identity);
Instantiate(s6,spawn6.position,Quaternion.identity);
  }
}

But what I actually want is, not to spawn them in every 7 seconds. I want to spawn new enemies when the other spawned enemies are dead. I mean, I don’t want more than 6 enemies in the scene at the same time. After all of the 6 enemies are dead, I want to spawn 6 more. After they are dead, 6 more and continue like this. I thought some thing like that: when all “enemy” tagged object are null, instantiate the others. But I could not find a way to do it, any ideas ? Thanks :slight_smile:

static var enemyCount : int;

    function Regenerate() {
    if(enemyCount <= 0){
    enemyCount = 6;
    Instantiate(s1,spawn1.position,Quaternion.identity);
    Instantiate(s2,spawn2.position,Quaternion.identity);
    Instantiate(s3,spawn3.position,Quaternion.identity);
    Instantiate(s4,spawn4.position,Quaternion.identity);
    Instantiate(s5,spawn5.position,Quaternion.identity);
    Instantiate(s6,spawn6.position,Quaternion.identity);
       }
      }
    
    function Update(){
    if(enemyCount <= 0)
    Regenerate();
    }

then on the enemies script:

var : dead = false;

function Die(){
if(!dead){
dead = true;
RegenScript.enemyCount--;

}

}

and just call Die() when he dies