- Home /
Highscore PlayerPrefs not working on iOS
I am having a problem with the playerprefs i setup for my score/highscore system. Currently, the problem is that (on iOS) when the player dies, the highscore does not show. This problem does not occur in the editor on my computer when i run the game. It's strange.
Here's my code for where the score system occurs in the play screen. I believe the problem has something to do with the PlayerPrefs.Save(), considering I barely know how it works exactly. Keep in mind I am relatively knew to Unity and C#.
void Update ()
{
//Score
if (moving == true) {
score += Time.deltaTime; //to reset highscore, set score = 0.01f
scoreText.text = "" + score.ToString ("f2");
}
//Set Player Score Pref
PlayerPrefs.SetFloat ("Player Score", score);
PlayerPrefs.Save ();
//Set High Score Pref
highscore = PlayerPrefs.GetFloat ("High Score", score);
if (score > highscore) { //to reset highscore, switch 'highscore' with 'resetHS'
PlayerPrefs.SetFloat ("High Score", score);
PlayerPrefs.Save ();
}
}
In my Game Over screen I show the score and the highscore. The score works on iOS, but the highscore does not. Here is the code I used for that.
void Start ()
{
PlayerPrefs.Save ();
finalscore = PlayerPrefs.GetFloat ("Player Score");
scoreText.text = "" + finalscore.ToString("f2");
PlayerPrefs.Save ();
highscore = PlayerPrefs.GetFloat ("High Score");
highscoreText.text = highscore.ToString("f2");
}
Thanks in advance for the help!
In line 14 of your first snippet.
highscore = PlayerPrefs.GetFloat ("High Score", score);
SHOULD BE
highscore = PlayerPrefs.GetFloat ("High Score");
You cannot pass a vaue to a getter function. :)
@digzou it's a default value that will be returned if PlayerPrefs had not been set. it's in the docs :| http://docs.unity3d.com/ScriptReference/PlayerPrefs.GetFloat.html
I think it's these so many PlayerPrefs.Save (); calls that's causing the problem, I mean to call it every frame is too redundant, even worse calling it in FiexedUpdate (like x50 times per second). probably ios is simply not catching up with the disk read/write and maybe even skipping the operation or something (I'm just guessing, not an expert here). read this: http://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html
also it's not wise to call GetFloat in every frame either, call it once and save the value in a local variable.
Your answer
Follow this Question
Related Questions
How to save a high score 2 Answers
Sorting Error 0 Answers
How can i get the following script back on track to being a single level high score saver 0 Answers
PlayerPref set int and get int 1 Answer
PlayerPrefs Highscore problem 1 Answer