PlayerPrefs & Android problem

Hi all,

I’m sure this has been answered before but the following code causes my android app to hang:

#if UNITY_ANDROID
int i = 0;
while (i < Input.touchCount)
{
if (Input.GetTouch(i).phase == TouchPhase.Began || Input.GetTouch(i).phase == TouchPhase.Stationary)
{

PlayerPrefs.SetInt(“scene”, 1);
PlayerPrefs.Save();
Application.LoadLevel(1);
}
++i;
}
#endif

It works fine in the editor when I run the same code for a key input and when I remove these lines
PlayerPrefs.SetInt(“scene”, 1);
PlayerPrefs.Save();
it works fine.

I don’t know whether its a permissions problem but there’s nothing in the docs to say it shouldn’t be called like this and its a bit of a headache really.

Thanks

It’s your input handling code that’s causing issues, not specifically Android or PlayerPrefs. Basically you’re telling Unity to save PlayerPrefs every frame times the number of touches. (ie. 30 FPS x 4 Touches = 120 Calls!) Solution for the hang is to make sure you are calling the PlayerPrefs code once, and only when you need to.