- Home /
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);
Answer by AngryMarine · Aug 29, 2011 at 04:05 AM
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.
$$anonymous$$ake sure you only call Die() once. The condition you set for Die might be valid multiple times while the function is called which accounts for the multiple deaths. I haven't seen your code but I would do something like this to ensure only 1 death.
var alive : boolean = true;
if(alive) {
//Do the stuff of the living
//Death Check Die(); function call
}
in your Die() {
alive = false; // should be the first line in Die()
Since you handle respawn in Die() make this your LAST line
alive = true;
Your answer
![](https://koobas.hobune.stream/wayback/20220612193414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fade on death! 1 Answer
Finding object via script 1 Answer
Help with player death/respawn coroutine? 1 Answer
How do I make my character die when hitting the terrain? 2 Answers