Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 coliam · Jan 02, 2013 at 07:45 PM · variablerestartvarhealth

Restart the game when Health variable reaches 0

This is the script I am using. I did not make this script! Basicly, I want the game to restart when the health reaches 0. I have very little scripting experience, but am happy to learn. Anyone know how I might be able to achieve this?

 @script ExecuteInEditMode()
 var healthTexture : Texture2D;
 var healthBorder : Texture2D;
 var health : int = 100;
 
 function OnGUI () {
     
     // At first we draw border/background texture (with current texture it isn't important)
     GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
 
     //Now on top we can put Health Bar texture
     var adjust : int = health * 3;                       //adjusting texture size (width) / health(100)
     GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15));
     GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
     GUI.EndGroup();
 }
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

4 Replies

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

Answer by Maulik2208 · Jan 03, 2013 at 05:57 AM

 void Update()
 {
    if(health <= 0)
    {
      Application.LoadLevel(/*here write number of the level (or name of the level) which you want to load*/)
    }
 }


Enjoy.....Cheers......IF found useful then don't forget to mark the answer......

Comment
Add comment · Show 2 · 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 coliam · Jan 03, 2013 at 06:18 AM 0
Share

Thanks! worked exactly how I wanted it! Didn't know it was that simple. Again, Thanks.

avatar image Maulik2208 · Jan 03, 2013 at 06:28 AM 0
Share

Glad that your problem is Solved.....have a happy coding....Cheers

avatar image
0

Answer by Piflik · Jan 02, 2013 at 07:56 PM

Do you want to restart the current level, or start over from the beginning?

Use Application.LoadLevel("Level"), where 'Level' is the name or index of the level you want to load (if you use the index, don't use quotation marks). If you want to restart the current level, you can write Application.LoadLevel(Application.loadedLevel).

Comment
Add comment · Show 5 · 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 coliam · Jan 02, 2013 at 08:03 PM 0
Share

What would I use to trigger this? I tested it out with a Input.Get$$anonymous$$eyDown.

function Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return)) {
Application.LoadLevel (0);
}
}

But how do I make this run when the health of the player reaches 0?

avatar image Piflik · Jan 02, 2013 at 08:08 PM 1
Share

Use whatever event you use to reduce the player's health. Just add an if statement to test, if the new health value is below 1 and if so, load the level.

avatar image coliam · Jan 02, 2013 at 08:22 PM 0
Share

I do not have an event to reduce the players health yet.

avatar image Piflik · Jan 02, 2013 at 08:30 PM 0
Share

Then you should take care of that first...you can't load a level when the player's health drops to 0, if the player's health is never reduced.

avatar image coliam · Jan 02, 2013 at 08:33 PM 0
Share

I know that. Thanks for the help anyway...

avatar image
0

Answer by ytanay · Jan 02, 2013 at 09:02 PM

Why not simply add an Update() function that checks if the health is below 0 and runs Application.LoadLevel() accordingly?

Here is the full code:

 @script ExecuteInEditMode()
 var healthTexture : Texture2D;
 var healthBorder : Texture2D;
 var health : int = 100;
 
 function OnGUI () {
 
     // At first we draw border/background texture (with current texture it isn't important)
     GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
 
     //Now on top we can put Health Bar texture
     var adjust : int = health * 3;                       //adjusting texture size (width) / health(100)
     GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15));
     GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
     GUI.EndGroup();
 }
 
 function Update () {
     if(health<0){
         Debug.Log("Player is dead");
             Application.LoadLevel("Your scene");
     }
 }

Hope this helps!

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 ytanay · Jan 02, 2013 at 09:05 PM 0
Share

If you want to restart the current level using this method, you could do Application.LoadLevel(Application.loadedLevelName)

avatar image coliam · Jan 02, 2013 at 09:07 PM 0
Share

How would I do that? (I dont have much experience with scripting)

avatar image ytanay · Jan 03, 2013 at 05:53 AM 0
Share

It's surprisingly simple. I updated my answer with the full code.

avatar image
0

Answer by Jozxyqk · Jan 03, 2013 at 06:17 AM

Before loading the level it'd be nice to give the player some time to realize what just happened. For that you could create a simple timer:

 float restartTimer;
 bool gameOver = false; //changing this to a more general "game state" later on will give more control
 void Update()
 {
     if (health < 0 && !gameOver)
     {
         //happens once, as state changes to game over
         restartTimer = 4.0f;
         gameOver = true;
     }

     if (gameOver)
     {
         gameTimer -= Time.deltaTime; //count down when in gameOver state
         if (gameTimer < 0.0f) //add '&& Input.GetButtonDown("Fire1")' to allow user to choose when to restart, but stops them accidentally clicking and missing a game over message
         {
             Application.LoadLevel(Application.loadedLevel); //or whatever level you want
             //update the state so LevelLoad doesn't keep being called. Probably not needed if this script gets destroyed anyway
             gameOver = false;
             health = 100;
         }
     }
 }

 ... then later in your OnGUI

     if (gameOver)
     {
         //draw game over message
     }

 ... Don't forget to disable some player controls so you can't keep playing when in the gameOver state


There may be other timing methods (I'm not a fan of the yield method) but this should work.

Comment
Add comment · Show 1 · 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 coliam · Jan 03, 2013 at 06:20 AM 0
Share

I thank you too, this is very useful.

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.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Acces to a var from another Script. 2 Answers

insert semicolon 2 Answers

set another scripts variable 2 Answers

How much memory does a pointer use? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges