Cant find out how to make a highscore

I can’t find a way to fix this script every time I play the game the score goes up normally but the high score doesn’t go up or change or do anything.

My code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Score : MonoBehaviour
{
    public Text field;
    public int currentDisplayScore = 0;
    public Text highScore;

    void Start()
    {
        highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
        StartCoroutine(CountUp());
    }

    IEnumerator CountUp()
    {


        if (currentDisplayScore > PlayerPrefs.GetInt("HighScore", 0))
        {
            PlayerPrefs.SetInt("HighScore", currentDisplayScore);
            highScore.text = currentDisplayScore.ToString();
        }

        while (true)
        {
            currentDisplayScore++;
            field.text = currentDisplayScore + "";
            yield return new WaitForSeconds(1f);
        }
    }

}

You are never updating your highscore in your “while(true)”, just move your if statement in the while loop

1 Like

Thank you so much