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 Geko_X · Oct 10, 2011 at 07:16 AM · guicolorcolour

GUI Colour based on Health?

Hey all,

I'm having some problems with my GUI. I have a health value displayed on screen, as a number out of 100 ("Health / 100" is displayed). When the health is high, I want the text to be coloured green. As it gets lower, I want it to get redder.

I have a script that I thought should work, but being new to Untiy and Java, it doesn't. It just is black no matter what. Can anyone help?

Heres the script that I'm using for health and the health display:

 var health : int = 100;
 var Player : Transform;
 var labelPos : Rect = Rect(20,10,300,50);
 var Skin : GUISkin;
 private var damage : int;
 private var damageRed : int;
 var Sound : GameObject;
 private var HColor = Color(1,1,1,1);
 private var Red : float;
 private var Green : float;
 
 function Start () {
     health = PlayerPrefs.GetInt("Health");
     SaveHealth ();
 }
 
 function Update () {
     OnGUI ();
     ApplyPDamage (damage);
     Green = health/100;                         //Here is colour values for red and green are worked out.
     Red = 1 - Green;                            //I think this is where the problem is
 }
 
 function OnGUI () {                            //Here is where the health is displayed
     HColor = Color(Red, Green, 0, 1);
     GUI.color = HColor;
       GUI.skin = Skin;
       GUI.Label(labelPos, "Health: " + health + " / 100");
 }
 
 function ApplyPDamage (damage : int) {
     health -= damage;
     if(health <= 0) {
         Die();
     }
     HurtSound();
     SaveHealth();
 }
 
 function Die () {
     Application.LoadLevel(1);
 }
 
 function HurtSound () {
     sound = Sound.GetComponent(AudioSource);
     sound.enabled = true;
     yield WaitForSeconds(1);
     sound.enabled = false;
 }
 
 function SaveHealth () {
     PlayerPrefs.SetInt("Health", health);
 }

Thanks in advance, Geko_X

PS: Is this the best way to do it, or is there another way?

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
3
Best Answer

Answer by syclamoth · Oct 10, 2011 at 07:42 AM

Instead of doing something like that, use Color.Lerp!

 var healthProportion : float = health / 100;
 Color currentColor = Color.Lerp(Color.Red, Color.Green, healthProportion);

That should give you the correct value.

Comment
Add comment · Show 4 · 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 Geko_X · Oct 10, 2011 at 08:00 AM 0
Share

Sorry, but where would this go?

I put it in the OnGUI function, and set the GUI colour to currentColor, and I get an error saying to put a semi-colon at the end, even though there is one.

avatar image Geko_X · Oct 10, 2011 at 08:20 AM 0
Share

Got rid of that error, and its still not working.

It works, kinda, but doesn't blend the colours. Its green for 100, but as soon as health gets to 99, its red.

Thanks for that!

avatar image syclamoth · Oct 10, 2011 at 08:34 AM 0
Share

Ok- you need to turn all your health values into floats! I think it doesn't know what to do with integer division.

avatar image Geko_X · Oct 10, 2011 at 08:39 AM 0
Share

Did that and it works!

Thanks so much!!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I make a colour editor UI? 1 Answer

Help with text color 1 Answer

Find brightness of certain part of screen 0 Answers

Get the color under the mouse cursor 2 Answers

problem changing color of text mesh (ios/android) 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