Hi,
In my game I have set up a lives system so that you have three lives and then you die. When I run the game the lives system will work most of the time. Other times the lives will not be removed or the game will play the game over scene before it is set to. Here is my 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;
}
}
That code looks fine. My best guess is that the bug is in how the lives are decreased.
If you set the lives to decrease by 1 every time there is a collision, then a collision that lasts more than 1 frame could cause multiple lives to be lost at once. That's just an example. You'll need to elaborate more on how lives are decreased and post some code to figure this out.
Update
I saw you posted this code:
private var dead = false;
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.tag == "Finish") {
dead = true;
//subtract life here
Health.LIVES -= 1;
}
}
You could set up an invulnerability period between collisions to prevent this bug:
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 = 3;//Wait at least 3 seconds between collisions
}
timeSinceLastCollision -= Time.deltaTime;
}
I'm not sure if you are running in the editor or building your game or what, but your LIVES are a static var and that means there is only one instance so if your player lost some lives then the level was loaded again, he would have the same number of lives as he did last time.
Or like Michael said, the error is more than likely in another part of your code that applies the loss of lives.