Reputation: 33
I'm new in Unity and I don't know how to click a button and increase the value of a slider.
I have created a slider and its value decreases over time. Now I want to increase that value every time I click a button.
public class BarraAlimento : MonoBehaviour
{
public const float alimentoMax = 100f;
public float alimento;
public Slider barraAlimento;
void Start()
{
alimento = alimentoMax;
}
void Update()
{
barraAlimento.value = alimento;
if (alimento <= 0)
{
alimento = 0;
}
else if (alimento > 0)
{
alimento -= Time.deltaTime;
}
}
}
That's the script of the slider, but I don't know how to do the button one.
Upvotes: 0
Views: 896
Reputation: 7189
You just need to declare a button and get the onClick event. After that, increase the value as you see fit.
public Button _yourButton;
void Start()
{
alimento = alimentoMax;
_yourButton.onClick.AddListener(ButtonClicked);
}
void ButtonClicked()
{
//increase your value as you wish
alimento += 1;
}
Upvotes: 3