• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Davidflynn · Aug 29, 2011 at 03:00 AM · respawndeathendgame

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);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Davidflynn · Aug 29, 2011 at 04:29 AM 0
Share

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.

avatar image AngryMarine · Aug 29, 2011 at 04:42 AM 0
Share

$$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;

avatar image Davidflynn · Aug 30, 2011 at 02:07 PM 0
Share

Ok thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

How to make an enemy respawn? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges