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;
}
}
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).