- Home /
why do i get 0
//var systime:int=10; var health:float=100;
function Update () {
if (Input.GetKey ("up"))
//systime =System.DateTime.Now.Hour;
{
var systime:int =PlayerPrefs.GetInt("systime");
PlayerPrefs.SetInt("systime", System.DateTime.Now.Hour);}
if (Input.GetKey ("down")){
print (systime);
}
}
Answer by aldonaletto · May 28, 2012 at 03:31 AM
Variables declared outside any function are member variables, and their lifetime is the same of the script; variables declared inside a function are temporary variables: their lifetime is the duration of the function (they are deleted when the function returns).
Since you commented out the systime declaration as a member variable and declared it inside Update, systime became temporary, and respawn each Update with value = 0.
Uncomment the first systime declaration and remove the one inside Update (let only systime = PlayerPrefs.GetInt("systime")), and things should start working.
Answer by hathol · May 28, 2012 at 03:27 AM
Looks like your systime variable is scoped to the "up" block and therefore non existent in the "down" one. Move the var systime:int =PlayerPrefs.GetInt("systime"); in the other block.