Loading new level after killing enemies

Hello i just started using unity3d, and i have some basic javascript expirience,what i want is when i kill all my enemies a new level should load, i used this:

function OnCollisionStay (col : Collision)

{

if(col.gameObject.name == "Bullet(Clone)")

{

    Application.LoadLevel("menu");

	Destroy(col.gameObject); 

	Destroy(gameObject);

}

}

it worked but it will load new level after killing only one enemy. How can i make it to load a new level after killing all 6 enemies?

function OnCollisionStay (col : Collision)

{
    if(col.gameObject.name == "Bullet(Clone)")
 
    {
 
        Destroy(col.gameObject); 

        numberOfEnemiesLeft--;

        if(numberOfEnemiesLeft == 0)
            Application.LoadLevel("menu");

        Destroy(gameObject);//This needs to be last, because if we destroy the gameObject before checking for the new level, that code might never get executed.
 
    }
}

You either need to set how many enemies you have in your scene or you need to count them manually, maybe with finding them by tag or something…

You can check if any more enemies exist, and if no more exist then you load the new level.

I don’t know the name of your enemy, and it’s possible you might have more then 1 enemy name so it would be beneficial to do your check by tag.

so for starters create a new tag, to do this click on any GameObject and in the inspector there is a drop down menu that says “Tag”. It is located right under the GameObject’s name. Click that and go to the button and click “Add Tag…”.

Under the tag array increase the size of the array by 1, and then edit the last index and put the tag “enemy”. This is done by clicking to the right of the “Element” (pretty fat right to be exact.).

Once this is done change your collision to this:

if(col.gameObject.name == "Bullet(Clone)"){
    var enemysLeft;
    enemysLeft = 0;
    for(var fooObj : GameObject in GameObject.FindGameObjectsWithTag("enemy")){
       enemysLeft++; //for every enemy left we will increase a var
    }

    Destroy(col.gameObject); 

    enemysLeft--;
    if(enemysLeft <= 0){ //We use <= because if it finds no enemies some how, decreasing 1 from 0 will result in -1.
       Application.LoadLevel("menu");
       Destroy(gameObject); //this probably won't ever get executed but unity will destroy it for us once loaded
    }


}

I am better at writing in C#, bu I think you get the ideea:

var NumberOfEnemies : int;
    
     function OnCollisionStay (col : Collision)
        
        {
        
            if(col.gameObject.name == "Bullet(Clone)")
             
            {
             
            NumberOfEnemies--;
             
            Destroy(col.gameObject);
             
            Destroy(gameObject);
             
            }
if(NumberOfEnemies == 0){
    
    Application.LoadLevel("menu");
    
    }

        }