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 valtteri_m · Sep 17, 2014 at 04:48 PM · javascriptguitexturehealth

Health Script not working

I have this health thing that theres a icon and text, if the health is over 50% its a certain picture and below 50% is another picture and max and health also have their own pictures. But if it goes down less than 50% the texture changes and if you go back up to over 50% the texture doesnt change back?? how do I fix this

Heres the code:

 var curHealth : int = 100;
 var mySkin : GUISkin;
 
 var HealthDead : Texture2D;
 var Health1 : Texture2D;
 var Health2 : Texture2D;
 var HealthMax : Texture2D;
 
 function OnGUI(){
      GUI.skin = mySkin;
     GUI.Box (Rect (10,10,92,116), curHealth.ToString() + "%");
     GUI.DrawTexture(Rect(20,40,72,72),HealthMax);
 }
  
 function Update () {
      
     if(Input.GetKeyDown(KeyCode.DownArrow)) {
          curHealth -= 10;
      }
      
      if(Input.GetKeyDown(KeyCode.UpArrow)) {
          curHealth += 10;
      }
      
      //Health Icon
     if(curHealth < 0) {
         curHealth = 0;
     }
     
     if(curHealth > 100) {
          curHealth = 100;
      }
      
      if(curHealth >99) {
          HealthMax = HealthMax;
      }
      
       if(curHealth <100 && curHealth >50) {
         HealthMax = Health2;
      }
      
      if(curHealth <50 && curHealth >0) {
         HealthMax = Health1;
      }
 }    
Comment
Add comment · Show 1
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 gjf · Sep 17, 2014 at 06:08 PM 0
Share

try using $$anonymous$$athF.Clamp() to limit the values between your max & $$anonymous$$. (being a c# practitioner i'm not 100% certain of the unityscript syntax)

this:

 if(curHealth >99) {
     Health$$anonymous$$ax = Health$$anonymous$$ax;
 }

does nothing. just delete it and pretend it never existed.

finally, when setting the texture - if you only have 2 total, then check for the conditions for one of them and use else to set the other. in this case, if your cutoff is 50 then test whether it's greater or equal only - no need for 2 test conditions. something like:

 if (curHealth >= 50)
 {
     Health$$anonymous$$ax = Health1;
 }
 else
 {
     Health$$anonymous$$ax = Health2;
 }

in c# i'd simply do something like:

 Health$$anonymous$$ax = (curHealth >= 50) ? Health1 : Health2;

but that's just me ;)

1 Reply

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

Answer by SuperLegendOfStarPrime3 · Sep 17, 2014 at 07:10 PM

You're changing HealthMax into all of the other different health images...

The solution is to use a new variable called HealthCurrent instead of applying it all onto HealthMax...

Here's a working version of the code:

 var curHealth : int = 100;
 var mySkin : GUISkin;
  
 var HealthDead : Texture2D;
 var Health1 : Texture2D;
 var Health2 : Texture2D;
 var HealthMax : Texture2D;
 var HealthCurrent: Texture2D = HealthMax;
 
 function OnGUI(){
     GUI.skin = mySkin;
     GUI.Box (Rect (10,10,92,116), curHealth.ToString() + "%");
     GUI.DrawTexture(Rect(20,40,72,72),HealthCurrent);
 }
  
 function Update () {
  
     if(Input.GetKeyDown(KeyCode.DownArrow)) {
         curHealth -= 10;
     }
  
     if(Input.GetKeyDown(KeyCode.UpArrow)) {
         curHealth += 10;
     }
  
     //Health Icon
     if(curHealth < 0) {
         curHealth = 0;
     }
  
     if(curHealth > 100) {
         curHealth = 100;
     }
  
     if(curHealth >99) {
         HealthCurrent = HealthMax;
     }
  
      if(curHealth <100 && curHealth >50) {
         HealthCurrent = Health2;
     }
  
     if(curHealth <50 && curHealth >0) {
         HealthCurrent = Health1;
     }
 }  



Hopefully that works. ;)

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do We Script a GameObject as a Child? 2 Answers

Object showing different when Build into Mobile 0 Answers

Press "E" to activate Object. Display Gui-texture or Text ?? 2 Answers

LoadLevel Health Question. 1 Answer

How to Show / Hide a GUI Texture 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