Level advancment Scirpt

hey guys, I'm making a fps, and am making a script that automatically goes to the next level when all enemies are dead. it goes like this:

var target : Transform;

function Update ()

{

if(target == null && GameObject.FindWithTag("Robot") == null);

Application.LoadLevel(1);

}

the problem is than when i attach the script to a game Object (Ive tried different objects) and play, it shows level 0 for 2 seconds, then loads level 1!

Ive tried modifying the tag and using Application.LoadLevel(Application.loadedLevel + 1); but it still doesnt work!

please help

You had a semicolon after the if, so it wasn't doing anything after the ifcheck. The Application.LoadLevel would always occur. Try this:

var target : Transform;

function Update ()

{
    if(target == null && GameObject.FindWithTag("Robot") == null)
    {
        Application.LoadLevel(1);
    }
}

Another thing to be wary of is that the Find/FindWithTag functions are quite intensive, and should generally not be used in Update() - as it is, you're searching every GameObject for one with the "Robot" tag every frame. You'd be much better off putting that in a seperate function and calling it when an enemy dies (to check if there are any left). For example:

function CheckForEnemies()
{
   if (GameObject.FindWithTag("Robot") == null)
   {
      Application.LoadLevel(1);
   }
}

You'd then need to hook this function up to where you apply damage. For example:

function ApplyDamage(damage : int)
{
   this.health -= damage;  // apply the damage that has been passed through

   if ( this.health <= 0 ) // if the enemy is now dead (<=0 health remaining)
   {
      CheckForEnemies();
   }
}

This is just an example - there may be a better way to hook it up, depending on the design/structure of your game. Other things to consider would be changing the LoadLevel from a static value to use a variable. You could have a "gameManager" object that stores a 'static variable' for the currentLevel, and then you could Application.LoadLevel(currentLevel+1), or put it in a function that checked if it was the last level, etc. Anyway, hopefully this will get you started.