0
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.

  • 2
    You're dividing two integers, which will result in an integer. If you want the result to be a float, cast one of the values to float. – Michael Roy Dec 04 '22 at 17:05
  • as Michael said - a division between two integers will be an integer. wither use (float)health.currentHealth/health.maxHealth or 1f*health.currentHealth/health.maxHealth – zambari Dec 06 '22 at 13:55

0 Answers0