- Home /
Variable not Updating
I have create a private variable in my script. I have set its value to 0. It is not in the inspector because it is private. But yet, when I apply this script:
function Update ()
{
print (thirst);
if (thirst < 100)
{
thirst = thirstRate * Time.deltaTime;
}
}
the variable remains 0 when printing it. This makes no sense but I must be doing something wrong...or it could be a glitch with Unity. Please help if you know anything! Am I putting this in a wrong function or something? Thanks!
Whole script:
#pragma strict
//Player variables
var thirst = 0; //100 = max, 0 = min.
var thirstRate = 0.08; //Increase thirst by...
function Update ()
{
//thirst
print (thirst);
if (thirst < 100)
{
thirst = thirstRate * Time.deltaTime;
}
}
Could you post the whole script that this update function is in?
is your thirstRate = 0 by any chance? :P
to see private variables in Editor. Either select Debug $$anonymous$$ode in the drop down located at the top right corner of Inspector window. Or assign [Serializable] attribute to your private var.
you need thirst to be a float type, so either do:
var thirst = 0.0;
or
var thirst : float = 0;
thirst is now an int. and Time.deltaTime is almost always below 1, so (int)Time.deltaTime = 0. thats why thirst does not increas.
the solution is using a float
Answer by Flightkick · Jun 21, 2015 at 05:37 PM
It's because you keep dividing the thirst.
Time.deltaTime is the time between each frame (and thus Update() call).
thirst = thirstRate * Time.deltaTime;
Will result in a very small result. I think you might be trying to increase thirst over time, if so you should try this instead:
thirst += thirstRate * Time.deltaTime;
Note the += instead of =
EDIT:
Like @Bunny83 mentions, the datatypes should also be defined as float because operations on the data will be rounded down. (i.e. 1 * 1.5 = 1 when using integers).
This and in addition the variable is an int variable. So it will most likely not change at all since the changed will be smaller than 1.
The "thirst" variable should be declared as float ins$$anonymous$$d of int.
Like this:
var thirst = 0.0;
ins$$anonymous$$d of
var thirst = 0;
Another case where UnityScript causes more confusion in beginner hands...
@Bunny83, Indeed, I didn't see that at first (used to C# with explicit na$$anonymous$$g conventions, I like to see the explicit datatype directly ins$$anonymous$$d of looking at the rest of the code to figure out what it holds). Thanks for adding, I'll update my answer.
Thanks everyone with the help, I knew it was something simple like that!
Answer by Socapex · Jun 21, 2015 at 12:34 PM
Maybe an int conversion? If the rate is too low it will floor to 0. Try thirst = (float)thirstRate * Time.deltaTime; just in case.
Also, a life changer thanks to TheAntRanch on twitter, if you put the editor in debug mode, you can see your private variables :O
Its really hidden, in the top top right drop down :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613201229im_/https://answers.unity.com/themes/thub/images/avi.jpg)