Hey,
It is quite hard to explain what I want but here it goes. First take a look at my script:
#pragma strict
var Food:UnityEngine.UI.Image;
var WaterDrop:UnityEngine.UI.Image;
var health = 100.0;
var hunger = 100.0;
var hungerSpeed = .1;
var thirst = 100.0;
var thirstSpeed = .2;
function Update ()
{
hunger -= Time.deltaTime * hungerSpeed;
thirst -= Time.deltaTime * thirstSpeed;
}
What I would like to happen is: as the hunger / thirst goes down, I would like the corresponding UISprites to change depending on the value. e.g. 100.0 = Bright Green. 00.0 = Bright Red 50.0 = Yellow and all the other colors inbetween, almost as if they are fading.
I hope I explained it well enough.
Sorry for bad english.
Thanks for help!
here is code but not tested but it work
var renderer:SpriteRenderer ;//drag your spriteObject
function Update()
{
//SpriteRenderer renderer = //Get the renderer via GetComponent or have it cached previously
if(health==100)
renderer.color = Color.green;
else if(health<=50)
renderer.color = Color.yellow;
else if (health==0)
renderer.color = Color.red;
}
For the color calculation you could do something like this (c#):
Starting color is green:
Color hungerColor = Color.green;
in Update():
if ( hunger >= 50.0f)
{
// While hunger value decreases (to 50%), add more red until you get yellow color (1,1,0)
hungerColor.r = 2.0f - ( 2.0f * hunger)/100.0f;
}
else if ( hunger > 0.0f) // don't go below zero
{
// Hunger value is below 50%, remove green component from color until you get red (1,0,0)
hungerColor.g = 2.0f * hunger / 100.0f;
}
and assign this color to the color component of your sprite