public class Health : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
}
public class HealthStatusBar : MonoBehaviour
{
public Health health;
public Image fillImage;
private Slider slider;
private void Awake()
{
slider = GetComponent<Slider>();
}
private void Update()
{
if (slider.value <= 0)
{
fillImage.enabled = false;
}//turns off slider if dead
float fillValue = health.currentHealth/health.maxHealth;
Debug.Log(fillValue);
if (fillValue < slider.maxValue / 3)
{
fillImage.color = Color.red;
}//changes colour if low on health
slider.value = fillValue;
}
}
I have these pieces of code and with - 'float fillValue = health.currentHealth/health.maxHealth;'. when current health is not the same as max health (e.g. 90 /100), the output just becomes 0. I have tested many times to see the change of current health so I know there isn't an issue there, it's just that it is not giving a float value as fill value.