- Home /
iphone variable values reset on application focus!?
Hi guys the weirdest thing is happening in my app.
So i have 2 simple variables declared in a script:
private var right : int;
private var wrong : int;
i increment them as necessary during the GAME scene, and when i return to the MENU scene, i do this right before loading it:
function LoadMenu(){
PlayerPrefs.SetInt("right", right);
PlayerPrefs.SetInt("wrong", wrong);
Application.LoadLevel(1);
}
In the menu i load those variables and do some calculation like so:
var right : int = PlayerPrefs.GetInt("right",0);
var wrong : int = PlayerPrefs.GetInt("wrong",0);
var percent : float = 0;
if((right + wrong) > 0){
percent = ((100.0*right)/(right+wrong));
}
rightText.text = "RIGHT: " + right.ToString();
wrongText.text = "WRONG: " + wrong.ToString();
percentText.text = percent.ToString("0") + "%";
nice and simple right?
and on ANDROID IT WORKS PERFECTLY EVERY TIME.
But on iphone, if a user accrues some right and wrongs, then changes windows midgame to another application for some time, and then comes back to the game and returns to the menu, the variables right and wrong are ZERO AGAIN!!!
This only happens if the app is not come back to for over 10 minutes or so, if u quickly lave and come back to the app, then everything works fine and the variables are saved.
Is this an iPhone bug? some kind of weird memory leak issue of some sort on iOS???
i can fix this problem by saving the right and wrong variables to playerprefs as soon as theyre incremented, but i just wanna know what the actual problem is so i can look out for it in future apps.
Please any help is greatly appreciated!
Answer by Bunny83 · Feb 12, 2015 at 11:38 AM
You should make sure to save your values on OnApplicationFocus. iOS as well as Android can dump idle Apps out of memory at any time the system wants to. Since you only save your values before you switch to your load menu the values aren't saved until then. If the player pauses your app midgame it's always possible that the game has to restart completely.
thats creepy... so not a single variable declaration is safe? how come other things don't mess up? seems weird...
basically i should run PlayerPrefs.Save on all my variables every single OnApplicationFocus
Lol, I don't know about iOS, but on android you often don't even receive OnApplicationFocus event. Applications on mobile platforms are highly depend on serialized state and can be killed at any moment like by inco$$anonymous$$g call/popup. So make sure to save some critical states immediately, for example current level/game stats.
Answer by MaxEden · Feb 12, 2015 at 11:17 AM
I believe, you forget to call PlayerPrefs.Save
o.O first time im seeing this! LOL thanks. id mark both of urs as answers if i could.
Your answer
Follow this Question
Related Questions
Large memory footprint increase when assigning a GUITexture from a WWW Object on iPad 1 Answer
How do I destroy things properly to free up memory? (iOS) 3 Answers
Clearing Editor References when Building for Mobile 1 Answer
Assets never unloading after loading next scene 1 Answer
lightmap vs normals 2 Answers