- Home /
Door open after destroying all the enemies.
Another question here. I have been looking for a solution for something like when i kill the enemies in a level, a door will open or something like that will be triggered. Not much discussion went through the community about this topic except i found just one. Here is the glimpse what was tried...
//Add the number of enemies already in the Level static var enemyCount : int = 0;
function Update () {
if(enemyCount < 1)
{
//Instantiate prize and open door
print("Ok Now Open The Door!");
}
if(enemyCount > 0)
{
print("The Enemy Count is" + "" + enemyCount);
}
}
I was wondering if anybody can help on it or already have a better way to do it. Even any link that is describing this matter is also appreciated as well.
Thank you.
Answer by Eudaimonium · May 11, 2015 at 08:19 AM
Beware about putting stuff such as "Instantiate prize" within an Update loop; in your example, enemies count will continue to be < 1 until you add more enemies, or load a new scene. Until then, you're instantiating prize every frame.
Instead, try this approach:
public int numberOfEnemies;
private bool needsPrize = false;
void Update()
{
if (numberOfEnemies > 0)
{
//print your number of enemies on screen
}
else
{
needsPrize = true;
print("You win bla");
}
//then, simply check:
if (needsPrize)
{
//Instantiate prize
//Don't forget this:
needsPrize = false;
}
}