- Home /
My health bars HealthBar.fillAmount doesnt change.tnx,i nead a help in fillamount :pls my HealthBar doesnt change. i dont know what to do.pls:)
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour {
public int MaximumHealth;
public int CurrentHealth;
Image HealthBar;
void Start()
{
CurrentHealth = MaximumHealth;
}
void Update()
{
if (CurrentHealth <= 0)
{
SceneManager.LoadScene("Menu");
}
HealthBar.fillAmount = CurrentHealth / MaximumHealth;
}
public void DamageToPlayer(int damageToGive)
{
CurrentHealth = CurrentHealth - damageToGive;
}
}
Is the "Image Type" of the HealthBar in the Image Component set to "Filled"?
Answer by shahar55g · Mar 01, 2018 at 05:13 AM
Yes.. @dishant27
In start method, HealthBar = gameObject.GetComponent<Image>();
Also check values of $$anonymous$$aximumHealth, CurrentHealth and damageToGive are assigned in the Inspector.
Answer by Derekloffin · Jan 18, 2020 at 08:35 AM
This is rather old, but I think the simple problem here is the use of integers. Both maximum and current health are ints, so the calculation for the division is also an int division, meaning it can only be 1 or 0. You'd need to either switch the type of the maximum and current to floats, or just cast them to float in the division op to force it to a float division instead.
Your answer
Follow this Question
Related Questions
Player health dropping far to fast 1 Answer
I need help with the player's health points 1 Answer
Health Bar decreases in both directions 0 Answers
How to make my health decrease? 1 Answer