- Home /
Question by
UniluckStudios · Jul 19, 2017 at 02:14 AM ·
playerprefssaveloadscore systemhighscore
Highscore won't save
The highscore will work when I reload the scene, but when I close the app, the highscore variable won't save and it will be set to zero. here is my code: public static int highscore; public static int score;// The player's score. public int value = 1;
Text text; // Reference to the Text component.
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
// Reset the score.
score = 0;
}
void Update (){
if (score > highscore) {
highscore = score;
}
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "Score: " + score + "\nHighscore: " + highscore;
}
void Start () {
PlayerPrefs.GetInt ("highscore", 0);
}
void OnDestroy () {
PlayerPrefs.SetInt ("highscore", highscore);
}
}
Comment
Answer by bobisgod234 · Jul 19, 2017 at 02:27 AM
PlayerPrefs.GetInt() returns an int, which you actually have to assign to your variable.
void Start () {
highscore = PlayerPrefs.GetInt ("highscore", 0);
}
Answer by VioKyma · Jul 19, 2017 at 02:28 AM
You need to call PlayerPrefs.Save()
void Start() {
highscore = PlayerPrefs.GetInt("highscore");
}
void OnDestroy () {
PlayerPrefs.SetInt ("highscore", highscore);
PlayerPrefs.Save();
}
Also what @bobisgod234 said. You need to assign the highscore to a variable.