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 :smiley:

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!