My health bars image.fillAmount doesnt change.
Im making a health bar and in the update function I set the image.fillAmount to be a variable I created. For some reason the health bar stays at 100 %. Can you tell me please why this happens please! Health_bar script: using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Health_bar : MonoBehaviour
{
public GameObject Enemy;
// Update is called once per frame
void FixedUpdate()
{
float EnemyHealth = Enemy.GetComponent<Enemyscripts>().Health;
Image image = GetComponent<Image>();
image.fillAmount = (EnemyHealth);
Debug.Log("Health : " + EnemyHealth);
}
}
By the way the GetComponent().Health is a variable in another script. Also the log is displayed with updated health every time health is lost. I tried Update() function but it didnt work either.
Answer by justaddice83 · Feb 28, 2020 at 10:19 AM
Image Type has to be "Filled", otherwise the fillAmount will be ignored and always be considered = 1.
Answer by CatStrategist · Nov 21, 2015 at 06:33 PM
image.fillAmout takes arguement between 0 and 1. I bet your EnemyHealth is value between 0 and 100 so you need to divide it by one hundred:
image.fillAmount = (EnemyHealth)/100;
Answer by Nathan1_ · Nov 21, 2015 at 06:34 PM
The fillAmount needs to be a value between 0 and 1, are you setting it using a percent? 50 for example would always be above 1.
Answer by CezaryDevelopment · Nov 21, 2015 at 05:45 PM
I fixed the problem my self. I forgot to divide my EnemyHealth by 100. public class Health_bar : MonoBehaviour { public GameObject Enemy;
// Update is called once per frame
void FixedUpdate()
{
float EnemyHealth = Enemy.GetComponent<Enemyscripts>().Health;
Image image = GetComponent<Image>();
image.fillAmount = (EnemyHealth/100);
Debug.Log("Health : " + EnemyHealth);
}
}
Yes this is because the fill amount goes from 0 to 1 not 0 to whatever health you have
Your answer
![](https://koobas.hobune.stream/wayback/20220612045905im_/https://answers.unity.com/themes/thub/images/avi.jpg)