Adding to variable only once?
I am trying to program a game over screen that triggers when the life counter reaches zero. The problem being adding three lives during the game over screen to prevent the game over screen appearing in an infinite loop. Whenever I try to add a variable when the game over screen appears, nothing happens much but the game over screen appears and repeats the scene opening in an infinite loop. I can't tell what I am doing wrong. Here is the code for both scripts.
Script 1
var livesincreased = false;
function Update()
{
if(GetComponent(LifeManager).Lives <= 0)
Application.LoadLevel ("GameOverScreen");
}
function Update2()
{
if(Application.loadedLevelName == "GameOverScreen")
GetComponent(LifeManager).Lives += 3;
GameObject.Find("PlayerLoops 1").GetComponent("LifeManager");
if(livesincreased == false)
{
LifeManager.Lives += 3;
livesincreased = true;
}
Destroy(gameObject);
}
function OnTriggerEnter (Objecte : Collider)
{
if (Objecte.tag == "Life")
{
GetComponent(LifeManager).Lives += 1;
}
}
Script 2:
static var Lives = 3;
var Health : int = GetComponent(HealthBar).Health;
function Update()
{
if (GetComponent(HealthBar).Health == 0)
Lives -=1;
}
function OnGUI()
{
GUI.Label (new Rect (99, 455, 2550, 1880), Lives.ToString());
}
Comment
Where are you calling "Update2"? Also, why you need to decrease your life by 1 if it is equal to 0 in script 2?
About the loading loop, it's because you are loading your level in update, loading multiple times the same level.
var isLoadingAlready = false;
function Update()
{
if(GetComponent(Life$$anonymous$$anager).Lives <= 0 && !isLoadingAlready)
{
isLoadingAlready = true;
Application.LoadLevel ("GameOverScreen");
}
}