- Home /
How do I display the PlayerPrefs?
I finally got my playerprefs right, but I don't know how display the best score on the game over scene. How do I do that?
Here is my playerprefs script:
function Update () {
//save highscore
if (GameMaster.currentScore > PlayerPrefs.GetInt ("Score")) {
PlayerPrefs.SetInt ("Score", GameMaster.currentScore);
}
}
Answer by iwaldrop · Jun 14, 2014 at 04:18 PM
You don't need to write to PlayerPrefs every frame, just do it when you want to change it.
function SetCurrentScore(int deltaValue)
{
currentScore += newValue;
PlayerPrefs.SetInt("Score", currentScore);
}
Then you'll need some logic to determine which score to show as the high score, which should look something like this.
Load HighScore from PlayerPrefs with a default value of 0.
Save the result of Mathf.Max(HighScore, CurrentScore) as highScore.
Set HighScore in PlayerPrefs to the value of highScore.
This should ensure that if you've just scored higher than ever before that you are showing the proper score, but that it is also being saved each time you finish a game.
Your answer
Follow this Question
Related Questions
Display Playerpref string in GUI.TextField. 2 Answers
reference variables within my xml file. 0 Answers
PlayerPrefs.GetInt only returns a 0 1 Answer
PlayerPrefs aren't working in iphone 2 Answers
Highscore GUI not working properly help 0 Answers