So basically I want to save the highscore of the player, so when it closes the game and launch it again the same number (the current highscore) will be displayed. I know that you can do it using PlayerPrefs, but I’m getting some trouble with it. My normal code, without the PlayerPrefs method, is this one (it works fine):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class Highscore Script : MonoBehaviour {
public static int highscoreValue;
Text highscore;
void Start () {
highscore = GetComponent<Text> ();
}
void Update () {
highscore.text = "Highscore: " + highscoreValue;
}
}
And then I tried to implement the PlayerPrefs, which ended in something like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class HighscoreScript : MonoBehaviour {
public static int highscoreValue;
Text highscore;
void Start () {
highscore = GetComponent<Text> ();
highscoreValue = PlayerPrefs.GetInt("Highscore");
}
void Update () {
highscore.text = "Highscore: " + highscoreValue;
PlayerPrefs.SetInt("Highscore", highscoreValue);
PlayerPrefs.Save();
}
}
The problem is that, with this new code, the highscore is always 0. And with that I also can’t really know if it is saving the highscore or not.