Lives Reference Not Working?

So heres the thing, I started my tutorial, everything was going good and then bam, this line doesn’t work, I have no idea why, there is a similar line above it and it works. The last one referencing Lives.currentLives doesn’t work.

var damage : int = 25;
var player : GameObject;
var loseLife : int = 1;
var spawnPoint : Transform;

function OnTriggerEnter (player : Collider) {
	player.gameObject.GetComponent(PlayerHealth).currentHealth -= damage;
	Debug.Log(player.gameObject.GetComponent(PlayerHealth).currentHealth);
}
function Update() {

	if(player.gameObject.GetComponent(PlayerHealth).currentHealth <= 0) {		
	player.transform.position = spawnPoint.position;	  
		Debug.Log(player.gameObject.GetComponent(PlayerHealth).currentHealth);
	    player.gameObject.GetComponent(PlayerHealth).currentHealth += player.gameObject.GetComponent(PlayerHealth).maxHealth;
	    player.gameObject.GetComponent(Lives).currentLives -= loseLife;
	}
}

Thanks
Khelton

1 Like

You don’t post the error, nor the code for the Lives component… makes it a bit hard to help.

BTW given that player is already a gameObject you probably don’t need to say player.gameObject…

Well no errors, the line just doesn’t work.

static var maxLives : int = 99;
static var currentLives : int = 5;

function Update () {
}

function OnGUI() {
	if(currentLives >= 0) {
	GUI.Box (Rect(Screen.width - 151, 0, 50, 50), "Lives");
		GUI.Label (Rect (Screen.width - 151, 10, 50, 20), "Lives: " + currentLives);
	}
}
1 Like