- Home /
Updating UISlider with value from variable.
Hi all,
I am using a slider to display a players current stamina. Very basic issue I am having here -
Why does this code:
if(currentStamina >= 0)
{
currentStamina += Time.deltaTime * 2f;
}
cause the UISlider to refill faster than this code:
if(currentStamina >= 0)
{
currentStamina += Time.deltaTime * staminaRegenRate;
}
even when staminaRegenRate is declared as a public float with a value of 2f? Also, where should I be making the statement:
staminaBar.value = currentStamina;
Currently I have it at the bottom of Update().
Thanks!
What makes you say the refill is faster. Sounds wrong to me.
Thanks Glurth - your comment led me to a solution - the issue was that I was changing the values in the class declarations where it was being defined, not in the inspector - which I learnt should be the case for public variables. The changes in $$anonymous$$onodevelop were not being fed back to the inspector, so each time I made a change in the regen rate variable in $$anonymous$$onodevelop, it was having no impact when the game was initiated - so it just used the values from the inspector, which never changed!
Thanks!
great! so, should I convert that "comment" to an "answer", or did it only lead you to look at something else? (sounds like we are talking about the same thing, but not sure)
By all means, convert it to answer! I would have done it myself but couldn't see an option to?
Answer by Glurth · Feb 13, 2015 at 03:21 PM
Make sure you assign the value to staminaRegenRate in start() or in the editor. If you assigned it as a default value in your class, that value will be overwritten by whatever value you have in the editor, at game launch.
Debug.Log() is your best friend when you are getting unexpecteed results like that. What does Debug.Log() say the actually value of staminaRegenRate is?
Update() should be a good place to update the slider value.
I would make a slight suggestion to change the slider value within the if. That way you don't update the bar every frame if it is full.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183214im_/https://answers.unity.com/themes/thub/images/avi.jpg)