How many enemys

hello everyone I am having trouble with keeping track of my enemys. Someone named Sheva helped me this morning but i was still having trouble. I want to have something keep track out of my enemys and have a var with a number value so i can make an if statement thats says if(numenemys >= 0){Application.LoadLevel(level2)}; can anyone help?

here is my script if it is any help

function Update () {
var numenemys : GameObject[ ];
numenemys = GameObject.FindGameObjectsWithTag(“Enemy”);
if(numenemys >= 0)
{
Application.LoadLevel(level2);
}
}

change “>” to “<” because you want for the enemies to be 0 to load next level right?

Ops my mistake but is this the right code correct? should this work because in unity its not and unity does not show any errors.

numenemys is a list, not a number. You’ll want something like numenemys.Count or numenemys.Length or something.

wait so set the var to something like var numenemys : int; ? then the next line have numenemys.Count;

No, it’s still an array of GameObjects. But comparing an array of game objects to a number is like asking “Is an apple greater than pi?”.

o well heres a script i have just got but it automatically loads level2

var enemyCount : int = GameObject.FindGameObjectsWithTag(“Enemy”).Length;
function Update ()
{
if(enemyCount <= 0)
{
Application.LoadLevel(“level2”);
}
}

You’re close, but you have two problems. First, you assign enemyCount it’s value exactly once, so it will never decrease. The second problem is you do your assignment outside of a function so it’s probably happening when the script starts and before the enemies are created and returning a value of zero.

so try something like this :

function Update ()
{
   var enemyCount : int = GameObject.FindGameObjectsWithTag("Enemy").length;

   if(enemyCount <= 0)
   {
      Application.LoadLevel("level2");
   }
}

That said, when you’re testing logic, you should generally comment out the active line (in this case it’s the LoadLevel) and use debug lines to figure out if it’s doing what you think it’s doing first.

I would not use FindGameObjectsWithTag(“Enemey”) each frame as this is a really slow operation.
A better approach would be to increment/decrement an integer value upon creation destruction of an enemey object.