I have a script where an object on collision updates the max hit points for the player. The value goes up but the maxValue on the slider does not go up.
I have added the “using unityEngine.UI”
but I cannot figure out the reference to the max value. How can I update the slider…
Object in project
Canvas
HealthBar
LevelManager.cs
void HealthIncrease(int exthealth)
{
LevelManager.instance.maxHealth += exthealth;
LevelManager.instance.healthBar.SetHealth(LevelManager.instance.currentHealth);
healthbar.cs
public class HealthBar : MonoBehaviour
{
public Slider slider;
public Gradient gradient;
public Image fill;
void Start()
{
slider.minValue = 0;
slider.maxValue = 7;
}
public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;
fill.color = gradient.Evaluate(1f);
}
public void SetHealth(int health)
{
slider.value = health;
fill.color = gradient.Evaluate(slider.normalizedValue);
}