Not losing lives

Hi, I’ve got two scripts here, for when my object falls off the edge - it’s meant to fall off the edge, respawn and lose a life, yet while Debug.Log (“Respawned Player 1”) fires, lose a life doesn’t. I’ve been racking my head on this a while :frowning: I’ve given the guitextures placeholders, but they aren’t changing. :frowning:

Lives 2 Player 1.js

private var dead = false;

    function OnCollisionEnter(hit : Collision)
    {
        if(hit.gameObject.tag == "DeadArea")
        {
           	dead = true;      
          	Lives1.LIVES1 -= 3;
        }
	}
    
function LateUpdate()


{
if(dead){
    transform.position = Vector3(40,16,13); 
    dead = false;
    Debug.Log("Respawned Player 1");
}
   else {
    Debug.Log("Alive");
}
}

Lives1.js

var Health1 : Texture2D;
var Health2 : Texture2D;
var Health3 : Texture2D;

static var LIVES1 = 3;

function Update () 
{
	switch(LIVES1)
	{
		case 3:
			Debug.Log ("1 life lost");
			guiTexture.texture = Health3;
		break;
			
		case 2:
			guiTexture.texture = Health2;
		break;
			
		case 1:
			guiTexture.texture = Health1;
		break;
		
		case 0:
			//Application.LoadLevel(0);
		break;
	}
}

Thanks

@Anne_Marije and @fafase already pretty much answered this in the comments, but here it is as an answer:

Well, I would put the debug.log in the
case2 because the case3 is the
starting point. The way you have now
is that it shows at starting and then
it stops showing when you lose a life.
Try instead to Debug.Log(LIVES1); to
see if the variable is changing.

Why are you doing -3 instead of -1?
Now LIVES1 becomes 3-3 =0 and in case
0 nothing is done. btw I thought I saw
this as answer earlier.

Yep actually that would be your
reason, didn’t see that one…

One more note:
The way you’re doing this (using Update) is needlessly processor expensive - you’re changing the guiTexture every single frame. It would be much better to put your switch within a public or static method and call that method when you want to update the LIVES1 count. Your LateUpdate is also very wasteful. Is there a reason you can’t just move that code up to where you’re setting dead = true? E.g.

    function OnCollisionEnter(hit : Collision)
    {
        if(hit.gameObject.tag == "DeadArea")
        {
            transform.position = Vector3(40,16,13); 
            Debug.Log("Respawned Player 1");    
            Lives1.LoseALife();
        }
    }

With that, you can get rid of the dead variable and the LateUpdate()

Your second file could then read

static var LIVES1 = 3;

static function LoseALife()
{
    LIVES1 -= 1;
    switch(LIVES1)
    {
       case 3:
         Debug.Log ("1 life lost");
         guiTexture.texture = Health3;
       break;

       case 2:
         guiTexture.texture = Health2;
       break;

       case 1:
         guiTexture.texture = Health1;
       break;

       case 0:
         //Application.LoadLevel(0);
       break;
    }
}

Apologies if this doesn’t work out of the box - I’m a C# programmer, not JS.

Thank you both :slight_smile: