- Home /
PlayerPrefs aren't working in iphone
In Keeping score and recording the score with this script:
var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;
function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin") { Debug.Log("Other object is a coin"); score += 100; scoreText = "Score: " + score; Debug.Log("Score is now " + score); Destroy(other.gameObject); }
}
function FixedUpdate(){ Debug.Log("OnTriggerEnter() was called"); score += 1;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
if (score > PlayerPrefs.GetInt ("Points")) {
PlayerPrefs.SetInt("Points", score);
} }
function OnGUI () { GUI.skin = mySkin; GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }
And I'M calling the "Points" Number with :
var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;
function Start() { PlayerPrefs.GetInt ("Points");
scoreText = "High Score: " + PlayerPrefs.GetInt ("Points");
}
function OnGUI () {
GUI.skin = mySkin;
GUI.Label (Rect (500, 10, 200, 500), scoreText.ToString()); }
-These scripts work in the unity editor. when the score is more than the best score it is changed. But when it is built and put on the device the score GUI isn't displayed and "Best Score" system doesn't work. How can i use fix this? Why isn't PlayerPrefs working on iPhone?
Im sorry about the code formatting I can't get it formatted no matter how hard I try.
-Thanks
Answer by Eric5h5 · Mar 06, 2011 at 07:16 AM
Remove the FixedUpdate function; it runs every physics frame, which is 50fps by default. That means you have 100 Debug.Log and 50 PlayerPrefs.SetInt calls per second, which is going to be extremely slow. You should use GetInt once (when starting), and SetInt once (when saving). The score only needs to be set when it changes. Only use FixedUpdate for moving physics objects or applying forces. Also, you don't need to use ToString for something that's already a string.
The High score isn't displayed on the iPhone, but it shows it in the editor. Why would this happen and how can i fix it?
Fix the code so it works properly first. Remember, call PlayerPrefs once, and store the results in a variable.
Answer by Tyler Starr · Mar 07, 2011 at 06:45 AM
I feel stupid, but the problem was that the GUI was in the camera view in the editor. And on the device it was out of camera view. All I had to do was move the GUI! Thanks Eric I changed my scripts in the way you suggested. Thanks.
Your answer
Follow this Question
Related Questions
how big is my app size 2 Answers
make game work on older iphone 1 Answer
useGUILayout=false does not honor GUI.depth 0 Answers
Exporting to Iphone, bad changes... 1 Answer