- Home /
Question by
Wolfos · Sep 30, 2011 at 06:00 PM ·
javascriptvariable
Variable remains 0
Probably pretty obvious question, but I'm learning Javascript by trying shit out (thus far, it works fine). My variable 'time' remains 0. It's probably because I say "public var time = 0" at the start of my program, but how else should I declare it?
public var time = 0;
function Update () {
time+= Time.deltaTime;
print(time);
}
function OnGUI(){
GUI.Box (new Rect(Screen.width-160, 10, 150, 40), "Time: " + time);
}
It's public because I will need to access it from my Reset() function (in another class) later.
Comment
Best Answer
Answer by Arbiter · Sep 30, 2011 at 06:43 PM
public var time = 0;
function Update () {
time+= Time.deltaTime;
print(time);
}
function OnGUI(){
GUI.Box (new Rect(Screen.width-160, 10, 150, 40), "Time: " + time);
}
It is because time is an int and Time.deltatTime is a float.
Simple change:
public var time = 0;
To:
public var time = 0.0;
And it should work perfectly now.
Answer by Wolfos · Sep 30, 2011 at 08:47 PM
Yep, that makes sense. I want to explicitly make it a float, so that would be "public var time:float = 0", right?
Your answer
![](https://koobas.hobune.stream/wayback/20220612232104im_/https://answers.unity.com/themes/thub/images/avi.jpg)