Hi everyone, very new to UI Sliders and Js and C# scripting.
I managed to increase and decrease UI Slider when health taken and gained.
But when it comes to increasing maxHealth UI Slider will not match that new maxHealth.
So, how do I go about matching UI Slider with the new maxHealth?
Do I have to make a new function and calling it something like mySliders(I tried but don’t know how to go about)?
Please show example code if possible, will be much appreciated.
My code examle:
What exactly are you trying to achieve?
Do you want to increase the maximum health by a second slider? Or by adjusting a float?
For the float variation:
using UnityEngine;
using UnityEngine.UI;
using SystemCollections;
public class Healthbar : MonoBehaviour {
public Slider healthBar;
public float maxHealth;
void Start{}
void Update{
healthBar.maxValue = maxHealth;
healthBar.minValue = 0;
DecreaseHealth();
}
public void DecreaseHealth ()
{
if(Input.GetKeyDown("e"))
{
healthBar.value -= 10;
}
}
or, if you want to use a second slider:
using UnityEngine;
using UnityEngine.UI;
using SystemCollections;
public class Healthbar : MonoBehaviour {
public Slider healthBar;
public Slider maxHealth;
void Start{}
void Update{
healthBar.maxValue = maxHealth.value;
healthBar.minValue = 0;
DecreaseHealth();
}
public void DecreaseHealth ()
{
if(Input.GetKeyDown("e"))
{
healthBar.value -= 10;
}
}