- Home /
How to reset lives after losing?
I am making a game. There is a main menu, first level and gameover scene. When you lose all your lives the gameover screen shows up. After a certain amount of time the gameover screen changes to the main menu. From the main menu the first level can be accessed. If I play the game, the transitions all work but if i go to the first level after losing all lives and going back to the main menu the gameover scene starts playing. How can I change the game so that it resets the lives after you lose?
Health script:
var health1 : Texture2D; //one live left
var health2 : Texture2D; //two lives left var health3 : Texture2D; //full health
static var LIVES = 3;
function Update () { switch(LIVES) { case 3: guiTexture.texture = health3; break;
case 2: guiTexture.texture = health2; break;
case 1:
guiTexture.texture = health1;
break;
case 0:
Application.LoadLevel (2);
break;
}
}
Die script:
private var dead = false;
private var timeSinceLastCollision = 0;
function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "Finish" && timeSinceLastCollision <= 0) { dead = true; //subtract life here Health.LIVES -=1; timeSinceLastCollision = 5; //Wait at least 5 seconds between collisions } timeSinceLastCollision -= Time.deltaTime; }
function LateUpdate() { if(dead) { transform.position = Vector3(2,6.5,2); gameObject.Find("Main Camera").transform.position = Vector3(0,0,0); dead = false; } }
Answer by Michael La Voie · Apr 12, 2010 at 09:34 PM
The problem is that you are using a static variable to hold the lives:
static var LIVES = 3;
It gets set once, when the script is created.
One alternative that allows you to keep it static is to reset it to three from the main menu when a player clicks "start game".
Just add a line:
ScriptName.LIVES = 3;
to the button event.
Answer by e-bonneville · Apr 12, 2010 at 09:35 PM
Use OnLevelWasLoadedlike this:
function OnLevelWasLoaded (level : int) {
if (level == 2) {
LIVES = 3;
}
}
Hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220612160950im_/https://answers.unity.com/themes/thub/images/avi.jpg)