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);
}
}
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.