UI image dissapears when using localScale.
I am trying to create a very basic health bar. I created a image and i set it so it's anchored to the bottom left and I can scale it. However, when I use localScale the image dissapears. This is my code: public class HealthBarScript : MonoBehaviour {
public GameObject PlayerObject;
private float maxHP;
private float currentHP;
private float HealthPercent;
private Image healthBar;
// Use this for initialization
void Start () {
healthBar = GetComponent<Image>();
maxHP = PlayerObject.GetComponent<PlayerController>().HealthMax;
}
// Update is called once per frame
void Update () {
currentHP = PlayerObject.GetComponent<PlayerController>().currentHealth;
HealthPercent = currentHP / maxHP;
healthBar.rectTransform.localScale = new Vector3(HealthPercent, 0f, 0f);
}
the bar only dissapears when I run healthBar.rectTransform.localScale = new Vector3(HealthPercent, 0f, 0f);
if I don't run that it doesn't dissapear. Any help is greatly appreciated!
Comment