Question by
jretchford · Dec 29, 2020 at 08:46 PM ·
playersliderpowerupupdatinghealth bar
Health slider not updating when health powerup is picked up
I have a player health system set up where the player can take damage, and the slider decreases, and when the player health reaches zero the slider will be at the bottom and the player will be dead. However, I have implemented some powerups that will give the player 15 health. The problem I have is that the health number manages to update on my "currrentHealth" integer, but it doesn't update on the health bar slider. So for those who might be a little lost, the currentHealth updates in the inspector (I have it as public so I can see it change), but the slider value doesn't change.
Here is my powerup code:
{
public int healthBonus = 15;
PlayerHealth playerHealth;
private void Awake()
{
playerHealth = FindObjectOfType<PlayerHealth>();
}
public void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
if (playerHealth.currentHealth < playerHealth.maxHealth)
{
Destroy(gameObject);
playerHealth.currentHealth = playerHealth.currentHealth + healthBonus;
FindObjectOfType<SoundController>().healthPickUp.Play();
}
}
}
}
here is my PlayerHealth code:
{
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
public void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
here is my HealthBar code:
{
public Slider slider;
public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(int health)
{
slider.value = health;
}
}
Comment