Health Bar image doesn't scale down when Player takes damage
This is my code for my Player. When I press U, I can see in the variable window that the player is taking damage, but the Health Bar doesn't scale down.
Edit: I've found that health, when referenced by healthBar.SetHealth and healthBar.SetMaxHealth, is a null value? What am I missing here?
public class Player : MonoBehaviour {
//variables
//verify that public floats can be updated through stat progression
public float maxHealth = 100;
public float maxStamina = 100;
public float maxMana = 100;
public float healthIncreaseRate, staminaIncreaseRate, manaIncreaseRate;
public float health, stamina, mana;
public HealthBar healthBar;
public StaminaBar staminaBar;
public ManaBar manaBar;
private bool dead = false;
//???
private bool fullHealth = true;
private bool fullStamina = true;
private bool fullMana = true;
// Start is called before the first frame update
public void Start()
{
health = maxHealth;
stamina = maxStamina;
mana = maxMana;
healthBar.SetMaxHealth(maxHealth);
staminaBar.SetMaxStamina(maxStamina);
manaBar.SetMaxMana(maxMana);
}
// Update is called once per frame
public void Update()
{
// if the player takes damage, health goes down
if (Input.GetKeyDown(KeyCode.U))
{
TakeDamage(20);
}
// Possible deaths
if (health <= 0)
Die();
//health, stamina, and mana increase over time
if (health < maxHealth)
{
health += healthIncreaseRate * Time.deltaTime;
}
if (stamina < maxStamina)
{
stamina += staminaIncreaseRate * Time.deltaTime;
}
if (mana < maxMana)
{
mana += manaIncreaseRate * Time.deltaTime;
}
}
public void Die()
{
print("You have died.");
dead = true;
}
public void TakeDamage(float damage)
{
health -= damage;
healthBar.SetHealth(health);
}
}
The HealthBar script is pretty basic: public class HealthBar : MonoBehaviour {
public Slider sliderHealth;
public void SetMaxHealth(float health)
{
sliderHealth.maxValue = health;
sliderHealth.value = health;
}
public void SetHealth(float health)
{
sliderHealth.value = health;
}
}
I'm not getting any errors or warnings. It's like I incorporated the HealthBar script incorrectly into my Player script, but I can't find anything wrong with my code. The sliders simply don't respond to anything that happens in the player script.
yeah, I don't see anything obvious in the script's reference to healthBar either.
If you put a debugger into healthBar.SetHealth, can you verify that the method is actually receiving the value you think you are sending?
Answer by streeetwalker · Sep 16, 2020 at 08:26 AM
You're right, you're missing a tiny thing. It's a mistake we all make often!
At the top of HealthBar.cs you have defined a Slider variable
public Slider sliderHealth;
Now click on the object in the hierarchy that HealthBar.cs is attached to.
In the script component HealthBar on that object, you will see a field named 'Slider Health' - I bet is says "none."
Make sure you drag the slider object in the Canvas hierarchy to that field.
Answer by judahcline · Sep 10, 2020 at 01:24 PM
if (Input.GetKeyDown(KeyCode.U))
{
TakeDamage(20);
}
needs to be:
if (Input.GetKeyDown(KeyCode.U))
{
TakeDamage(20f);
}
a float will take a number as 0 if there is not an f behind it
That's not true - the int will be cast as a float. Try it yourself.
Your answer
Follow this Question
Related Questions
Anim object with Relative Position 1 Answer
Can I assign no value to a variable like Gamemaker? 1 Answer
Convert int to binary 1 Answer
Help with a health bar 0 Answers