Make Deaths end game

I have this first code build on to my player im wanting to make it so the player has a set amount of respaws. Once the player has reached that amount of respawns the game will load game over.

function Die(){
	//play death sound if it exists
	if(deathSound)
	{
		audio.clip = deathSound;
		audio.Play();
	}
	print("dead!");
	playerController.isControllable = false;
	
	animationState = GetComponent(Widget_Animation);
	animationState.PlayDie();
	yield WaitForSeconds(animation["Die"].length -0.2);
	HideCharacter();
	
	yield WaitForSeconds(1);
	
	//restart player at last respawn check point and give max life
	if(CheckPoint.isActivePt){
		controller.transform.position = CheckPoint.isActivePt.transform.position;
		controller.transform.position.y += 0.5;   //so not to get stuck in the platform itself
	}
	ShowCharacter();
	health = maxHealth;

I would like to make it so that it shows on screen with this some how.

        GUI.Label (Rect (25, 25, 100, 30), "Life: "+Life);

1 Answer

1

Here ya Go Assuming 25 respawns. Code:

var life : int = 25;

function OnGUI () {

    GUI.Label (Rect (25, 25, 100, 30), "Life: " +life);

}

Then inside your death function add these two lines.

life -= 1;

if(life == 0) {GameOver(); return;}

From here you can make your GameOver() function be whatever you like.

That works thanks now I just have to find out why each time I get killed the bunnies are killing me another 4-7 time during the play dead code.

Ok thanks