- Home /
Change colour of guiTexture from green to red.
I have a heath bar as guiTexture
with green colour, which scales down as per the available health.
//health is updated like so
health = health/100f;
//healthBar is updated like so
rect = healthBar.pixelInset;
rect.width = originalWidth*health;
healthBar.pixelInset = rect;
This works fine.
How to change it's colour as well according to available health?
e.g. 100% health - green colour, 50% health - green to red colour less than 10% health - red colour
I have tried with,
Color.Lerp(Color.green, Color.red, health - 1);
But I am not familiar with Color.Lerp;
Answer by robertbu · Mar 02, 2014 at 06:02 AM
You can set a GUITexture's color using 'GUITexture.color'. I believe that color is an overlay, so you'll want your bar texture to be white. Color.Lerp() takes a value between 0 and 1 for the last parameter, so your code might be:
guiTexture.color = Color.Lerp(Color.red, Color.green, health);
This assumes that health goes from 1.0 (healthy) to 0.0 (dead?).
This directly changes to Red colour. I am reducing health by -0.1.
You need to pass values between 0.0 and 1.0. Post the code you are using.
Your answer
Follow this Question
Related Questions
Solid color flash entire screen 2 Answers
guitext and texture following gameobject, how to destroy and alternatives? 1 Answer
changing guiTexture color with raycast 1 Answer
Disco like GUITexture? 1 Answer
guitext is not changing color 1 Answer