- Home /
High Score not retrieving
Hi
I am trying to setup a high score system in Unity. On the first load of the game the high score will show, but when you restart the game the high score is reset and then nothing gets displayed. My code is below. What am I doing wrong?
public class Score : MonoBehaviour {
public GUIText scoreText;
public GUIText bestText;
public int ballValue;
private static int score = 0;
private static int bestscore;
// Use this for initialization
void Start () {
bestscore = PlayerPrefs.GetInt("High Score");
score = 0;
UpdateScore ();
}
void OnTriggerEnter2D () {
score += ballValue;
UpdateScore ();
}
void UpdateScore () {
scoreText.text = "SCORE: " + score;
if (score > bestscore) {
bestscore = score;
PlayerPrefs.SetInt("High Score", bestscore);
bestText.text = "BEST: " + bestscore;
}
}
}
Thanks
Answer by GameVortex · Jul 04, 2014 at 11:56 AM
PlyerPrefs code looks fine to me.
The problem I see is that bestText.text is only updated when score is greater than bestscore, which it is not at the start of the game. This means that in your Start method you load the highscore but do not update the GUIText with the highscore so it displays the default value which is probably 0.
I recommend duplicating line 26 and adding it to your start method as well, right after you load the highscore.
Answer by pacific00 · Jul 04, 2014 at 12:26 PM
try adding
bestscore = PlayerPrefs.GetInt("High Score");
as the first line of UpdateScore ()
Your answer
![](https://koobas.hobune.stream/wayback/20220613153230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Taking high score from anther script and then displaying it 2 Answers
A node in a childnode? 1 Answer
Can anyone tell me what is wrong with my high score displaying script? 3 Answers
Score going up every second 1 Answer
Highscore Save 0 Answers