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

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?


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!

I believe, you forget to call PlayerPrefs.Save

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.