I am new to unity and trying to add a high score to my game. I’ve been at this for two hours, and feel like giving up. I would love some help on this.
Right now I have a score display script, which is separate from my high score script and displays my current score + works perfectly, and transfers from scene to scene. My high score script does the exact same thing that me score display script does, even though I want it to keep and maintain a high score instead. Here is the c# script of my score display:
TextMeshProUGUI scoreText;
GameSession gameSession;
// Start is called before the first frame update
void Start()
{
scoreText = GetComponent<TextMeshProUGUI>();
gameSession = FindObjectOfType<GameSession>();
}
// Update is called once per frame
void Update()
{
scoreText.text = gameSession.GetScore().ToString();
}
And here is my high score script:
TextMeshProUGUI highScore;
TextMeshProUGUI score;
GameSession gameSession;
// Start is called before the first frame update
void Start()
{
highScore = GetComponent<TextMeshProUGUI>();
score = GetComponent<TextMeshProUGUI>();
gameSession = FindObjectOfType<GameSession>();
highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
}
// Update is called once per frame
void Update()
{
int number = gameSession.GetScore();
score.text = number.ToString();
if (number > PlayerPrefs.GetInt("HighScore", 0))
{
PlayerPrefs.SetInt("HighScore", number);
highScore.text = number.ToString();
}
}
both of these scripts call the number from a different script, called game session to actually get the score. Let me know if you need to see that script too, otherwise, please help me figure out how to make this work!