playerLives

I apologize if this is seems like a stupid question but coding is very new to me. I did a little searching and found some code that partially works for what I am trying to do but I cant get it all to work. I have this code all working except for subtracting from the playerLives. What am I do wrong?

var playerLives: int;

function Update () {

if(transform.position.y <= 0.3){

	
	Destroy(gameObject);
	
	playerLives --; 

}
}

Thanks

try first subtract lives, then destroy object:

var playerLives: int;

function Update () {

if(transform.position.y <= 0.3){

   playerLives --;

   Destroy(gameObject);

}
}

Changing the order didn’t do anything.

That makes no difference, since Destroy doesn’t take effect until the end of the current frame anyway. The problem is that you are destroying the object that playerLives is on, so naturally the script, along with the variable, won’t exist anymore.

–Eric

I was thinking about it and I have a tag set on my object. Is there a way I can say if gameobject with tag " " is destroyed then subtract from playerLives?

You probably want to have a manager object that keeps global variables, so you can call things like “manager.playerLives–” as necessary.

–Eric

Thanks for your help Eric. I will look into that a bit because I have no clue what you just said.