Question by
codedbythet · Mar 31, 2021 at 08:40 PM ·
sliderscriptingproblemscriptable objecthealthbarhealth
Canvas Slider Max Value / High Value Help
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);
}
Comment
I tried adding this in the Slider script and the player health goes up but the slider maxvaluse does not change.
if (LevelManager.instance.maxHealth > slider.maxValue)
{
slider.maxValue = LevelManager.instance.maxHealth;
}
Answer by codedbythet · Mar 31, 2021 at 09:52 PM
Figured it out. Had the code in the wrong position on the slider.
public void SetHealth(int health)
{
slider.value = health;
fill.color = gradient.Evaluate(slider.normalizedValue);
if (LevelManager.instance.maxHealth > slider.maxValue)
{
slider.maxValue = LevelManager.instance.maxHealth;
}
}