- Home /
PlayerPrefs issue
Hi. I'm doing a simple 2D game and i have put a score and a high score values in game over screen.
During testing, I made my own high score that persists in the game built so, as a solution, I used PlayerPrefs.DeleteAll() to delete the high score value and then I remove the function from the script, but now my high score always shows "0".
Here is the function to show the high score:
function HighScore () {
//simply to save high score
if(score > PlayerPrefs.GetInt ("highScore")) {
PlayerPrefs.SetInt("highScore", score);
}
}
and here is the function to show it:
function OnGUI () {
GUI.skin.label.font = font;
GUI.contentColor = Color.black;
GUI.Label (Rect (10, 40, 600, 40), "High Score = " + PlayerPrefs.GetInt("highScore"));
}
I put the PlayerPrefs.DeleteAll(); in a button of my gameOver screen, like that:
function OnMouseDown () {
Application.LoadLevel("menu");
Destroy(GameObject.Find("Score"));
PlayerPrefs.DeleteAll();
}
and, once the value was removed, I erase it from the script.
Any idea of how recuperates the normal behaviour of the gameObject? :'(
Thank you so much!!!
Have you checked if it is going inside function HighScore()?
paloaragon: Ok. i faced exact this problem and solved .. your PlayerPrefs value will be update if score value greater than 0. Are you check this value ?
Also you have to call PlayerPrefs.Save() method after setting new value. You have to give one try to this also.
You don't need PlayerPrefs.Save() to store a value during playtime, only between sessions.
The problem with the code seems to be the if statement on line 4 is not returning true. If PlayerPrefs.GetInt ("highScore")
is not set, it will return 0.