end game after wave

i would like to know how to end my game after my enemy waves have finished here is my wave code:

var spawnPoints : Transform[];  // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies = 20;  // Total number of enemies to spawn.
var yieldTimeMin = 2;  // Minimum amount of time before spawning enemies randomly.
var yieldTimeMax = 5;  // Don't exceed this amount of time between spawning enemies randomly.

function Start(){
    Spawn();
}

function Spawn(){ 
   for (i=0; i<amountEnemies; i++){ // How many enemies to instantiate total
      yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.

      var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
      var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.

	  Instantiate(obj, pos.position, pos.rotation); 
   } 
}

private var temp : int = 0;

function Spawn()
{
     for (i=0; i<amountEnemies; i++)
     {  
      // How many enemies to instantiate total yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax
       temp++;
     }
}  

take a private var as the counter(here in the above example temp)and update its value.The moment it reaches your desired count do whatever you want ,load a new scene,quit the application anything you wanna do

suppose i want to load a new scene after 20 enemies have instantiated

function update()
{
     if(temp == 20)
    Application.LoadLevel("My Desired Level");
}    

}

We can't give you an exact answer as we don't know what type of game it is. I would check for `if (i==amountEnemies)` and if there are any enemies left or just yield a specific amount of time after that. You could probably do this in your for-loop at the very end or after your for-loop.

So in a very basic example:

static var enemiesAlive : int = 0; //Keeps track of how many enemies there are

function Spawn(){ 
   for (i=0; i<amountEnemies; i++){ // How many enemies to instantiate total
      yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.

      var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
      var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.

      Instantiate(obj, pos.position, pos.rotation);
      enemiesAlive++ //Add to how many enemies are alive
      if (i==amountEnemies) InvokeRepeating("endGame", 0, 1); //When all enemies are instantiated check if there are any enemies alive every second
   } 
}

function endGame () {
    if(enemiesAlive==0)
        Application.LoadLevel(Application.loadedLevel+1); //Load next level in the level list
}

Then when you destroy an enemy you also subtract from `enemiesAlive`:

SpawnScript.enemiesAlive--

Also notice that you must destroy enemies that isn't longer needed (passed the screen?). If your game isn't structured like that, then don't use enemiesAlive and InvokeRepeating, instead just call endGame() and do this:


    function endGame () {
        yield WaitForSeconds(5); //The amount of reasonable time to wait before next level
        Application.LoadLevel(Application.loadedLevel+1);
    }