In the current game I am creating I am attempting to have a dynamically changing health bar color corresponding to the current health of the enemy. When the health is at max the bar will be green (0, 255, 0), and when at low health the bar will be close to red (255, 0, 0). I coded the function to change from green to red depending on the current health of the enemy. When the enemy is at half health the color should be (125, 125, 0), however whenever I start the game the enemy begins at full health (green) and once the health is no longer at max, the bar is the same yellow until the enemy is dead. What part of my code makes it so that Unity cannot chance color simultaneously?
public void Start()
{
health = maxHealth;
healthBar.fillAmount = 1;
}
public void Update()
{
canvas.transform.LookAt(tranformTarget);
healthBar.fillAmount = health / maxHealth;
greenColor = (int)(255 * healthBar.fillAmount);
redColor = (int)(-255 * healthBar.fillAmount + 255);
Color healthBarColor = new Color(redColor, greenColor, 0, 255);
healthBar.color = healthBarColor;
Debug.Log(greenColor);
Debug.Log(redColor);
}