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 bowman290 · May 10, 2012 at 11:03 AM · playerhealthbarliferegenerationhealth

Regain health on GUI

Hi all i have a pretty simple script linked up to my player and when the enemy hits my player he loses health and it is visible through the GUI. However the tiny if statement at the bottom is supposed to regenerate his health over time only it doesn't or at the very least isn't being shown by the GUI to do so. Any help would be much appreciated.

 var h00: Texture2D;
 var h10: Texture2D;
 var h20: Texture2D;
 var h30: Texture2D;
 var h40: Texture2D;
 var h50: Texture2D;
 var h60: Texture2D;
 var h70: Texture2D;
 var h80: Texture2D;
 var regen : float = 5;
 
 static var HEALTH = 80;
 
 function Update () 
     {
 
     var g_Health = gameObject.Find("g_health");
 
     if(HEALTH == 70){
     
         g_Health.guiTexture.texture = h80;
         return;
     }
     
     else if(HEALTH == 60){
     
     g_Health.guiTexture.texture = h70;
         return;
     }
     
     else if(HEALTH == 50){
     
     g_Health.guiTexture.texture = h60;
         return;
     }
     
     else if(HEALTH == 40){
     
     g_Health.guiTexture.texture = h50;
         return;
     }
     
     else if(HEALTH == 30){
     
     g_Health.guiTexture.texture = h40;
         return;
     }
     
     else if(HEALTH == 20){
     
     g_Health.guiTexture.texture = h30;
         return;
     }
     
     else if(HEALTH == 10){
     
     g_Health.guiTexture.texture = h20;
         return;
     }
     
     else if(HEALTH == 5){
     
     g_Health.guiTexture.texture = h10;
         return;
     }
     
     else if(HEALTH == 0){
     
     g_Health.guiTexture.texture = h00;
         return;
     }
 
 HEALTH += regen * Time.deltaTime;
 
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by zz74b · May 10, 2012 at 11:19 AM

Just a few pointers for you:

Once you have set your texture, you return from the function. Your regen code will never be run if the health == any of your statements...

your else if statements will only change the texture if the health is EXACTLY the value specified.

Health looks like it will be an integer (assuming the code above is c#) in which case the regen * Time.deltaTime will be a very small number (i.e. 0.000025). The following maths will then occur:

cast regen health to integer => closest integer to 0.000025 == 0 add 0 to Health.

That's probably why your health never regenerates.

To fix:

Remove your return statements from within your if statements.

Change the var Health = 80; to be:

 float Health = 80f;

but then you must be aware that is it unlikely that your else if(health == x) values will EVER be true. And you'll need to change them to else if (Health < 40) etc.

Edit: Looks like you are using JS - You'll have to convert the syntax above.

Comment
Add comment · 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
0

Answer by Fabkins · May 10, 2012 at 11:42 AM

As the poster said the health value is going to default to an integer and you adding a float. Changing it to a float will fix the health regen but will mess up your if statement. So you could do the following:

 static var HEALTH:float  = 80;
 
 function Update () 
 {
 var tempHealth:int = HEALTH;


 if(tempHealth == 70){
    g_Health.guiTexture.texture = h80;
    return;
 }
 /*    the other if statements using tempHealth */
 
 HEALTH += regen * Time.deltaTime;
 }

Also for effeciency , I would move the getObject call out of the update code to the statup:

 function Startup()
 {
 g_Health = gameObject.Find("g_health");
 }

Obviously make g_Health a global.

Lastly you could put all the bitmaps into an array and reference the array. Something like:

 var healthTexture: Texture2D[];

Then instead of having any "if" statements just do:

 var tempHealth:int = HEALTH;
 g_Health.guiTexture.texture = heatlTexture[tempHealth/10];

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 Fabkins · May 10, 2012 at 11:47 AM 0
Share

Note the above is based on having 10 textures rather than 9, so adjust accordingly.

Lastly, if the health bar is just the typical kind of tube strip, you could consider rescalling the graphic ins$$anonymous$$d. Obviously if you have something more elaborate than your solution stands.

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

6 People are following this question.

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

Related Questions

Life Player loads another level 1 Answer

Player Heal *Help* 0 Answers

I need help with the player's health points 1 Answer

Spinning Pointer that reacts when health drops 0 Answers

How to make a camera's background be another camera's veiw 1 Answer


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