• 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 androids · Jun 17, 2013 at 12:43 AM · colorchangehealthbarhealth

HealthBar with ColorChange

I have a Healthbar but I want it to change of color.

The HealthBar is decreasing by delta.Time. But I want the HealthBar color is blue and if it has less health, the color will change in red. And if I fill the healthbar with medic Objects, it turns back to green.

By example, if the maximum health is 100 then the healthbar color is green, if it's 30 then the bar color will change to red.

Someone who can help me?

This is what I got,

 static var totalTime = 100.0;
 static var remainingTime = 100.0;
 var BarBorder:Texture2D;
 var BarBorderWidth:float=800;
 var BarBorderHeight:float=28;
 var Bar:Texture2D;
 var BarWidth:float=800;
 var BarHeight:float=28;
 
 
 function Update() {
     if (remainingTime > 0.0)
        remainingTime -= Time.deltaTime;
    
 }
 
  
 function OnGUI(){
     GUI.DrawTexture(Rect( (Screen.width - BarBorderWidth)*0.5,(Screen.height - BarBorderHeight)*1.0, BarBorderWidth, BarBorderHeight), BarBorder);
     GUI.DrawTexture(Rect( (Screen.width - BarWidth)*0.5,(Screen.height - BarHeight)*1.0, BarWidth* remainingTime/totalTime, BarHeight), Bar);
 }

This is with guiTextures. But I really want it with colors. Please help :D

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

Answer by xt-xylophone · Jun 17, 2013 at 05:05 AM

I havnt drawn Textures in a while but from memory:

You should be able to go

 GUI.color = aColorValue;

For setting up the colour you want you can use some if/elses to choose what you need, i.e.

 Color barColour;
 if(bar > 30) {
     barColour = Color.red;
 } else {
     barColour = Color.green;
 }

Or if you want to do some more complicated stuff, dont use an if/else. Make some code to do a colour change slowly. You can set the RGB of colours and are in a range of 0-1 of each RGB. If you want green to red. Create a colour that is set to (0,1,0) and then based on how far along your bar is, turn it to (1,0,0)

If you can calculate the %(a value from 0-1) of your bar, you could use this math:

 colour.red = 1 - percentage;
 colour.green = percentage;

This means as your percentage bar goes up from 0-1, the red colour will go from max to zero and the green will do the same. At 0% bar, its fully red. At 100% bar its fully green. If you wanted it the other way around, just swap the math

Hope that helps!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image

Related Questions

3D Text - How to change colour - Doesn't seem to work 2 Answers

How to change color as flashing or blink? 0 Answers

Health below zero but shows negative numbers 1 Answer

Horizontal Slider Normal-Background Change Color Independently 3 Answers

Display Health Through Already Working Health Script 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