- Home /
OnBecameInvisible activates upon level load.
Basically, I have a problem where OnBecameInvisible activates when I load the next level, and I can't have that because it ends up adding 2 points to the score instead of just 1.
Does anyone know how I can fix that?
relevant part of the script:
if (health < 1) {
addEnemyScore();
}
}
void OnBecameInvisible() {
addEnemyScore ();
}
void addEnemyScore() {
dfScore.p2Score = dfScore.p2Score + 1;
dfScore.winner = 2;
Application.LoadLevel(5);
}
Answer by karl_jones · Jun 25, 2015 at 10:26 PM
You could use a flag to indicate the level is loading and reset the flag once the level is loaded. Something like this:
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html
bool loading = false;
void OnBecameInvisible()
{
if(!loading)
addEnemyScore ();
}
void addEnemyScore()
{
dfScore.p2Score = dfScore.p2Score + 1;
dfScore.winner = 2;
loading = true;
Application.LoadLevel(5);
}
void OnLevelWasLoaded(int level)
{
loading = false;
}
Thank you so much! It worked perfectly, now it works :D Although for some reason it inverted the "winner" variable, but I can just flip them back anyway. Thank you:
Your answer
Follow this Question
Related Questions
#if not working after api update 1 Answer
Compiler Warnings Are Now Errors 0 Answers
How to import an fbx model properly from softimage 0 Answers
Aplication.LoadLevel isn't working in built version 2 Answers