High score counter is resetting when dead

So I’ve managed to get a score counter and a highs core counter up on my game (so the longer you survive the more points) but every time i die the highs core count is not saved and i cant understand why. Here’s both scripts relating to the scores:

    public Text scoreText;
    public float scoreCount;
    public Text highscoreText;



    public float pointsPerSecond;

    public float highscoreCount;

    public bool scoreIncreasing;

    void Update()
    {
        if (scoreIncreasing)
        {
            scoreCount += pointsPerSecond * Time.deltaTime;
        }
     

        if (scoreCount > highscoreCount)
        {
            highscoreCount = scoreCount;
        }

        scoreText.text = "" + Mathf.Round(scoreCount);
        highscoreText.text = "High Score: " + Mathf.Round(highscoreCount);
         
    }
  private ScoreManager scoreManager;
    public float slowMoTime = 10f;

    private void Start()
    {
        scoreManager = FindObjectOfType<ScoreManager>();
    }
    public void EndGame()
    {
        StartCoroutine(RestartLevel());

    }

    IEnumerator RestartLevel ()
    {
        scoreManager.scoreIncreasing = false;
        Time.timeScale = 1f / slowMoTime;
        Time.fixedDeltaTime = Time.fixedDeltaTime / slowMoTime;

        yield return new WaitForSeconds(1f / slowMoTime);
     
        Time.timeScale = 1f;
        Time.fixedDeltaTime = Time.fixedDeltaTime * slowMoTime;
     

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

        scoreManager.scoreCount = 0f;
        scoreManager.scoreIncreasing = true;


    }

Assuming you call RestartLevel() when you die, this is normal. When you reload a scene all the objects in that scene get reloaded. Nothing is stored implicitly between scenes, except when you call DontDestroyOnLoad() on an object. If you want to keep your score manager working between scenes you would need to write a system to make that happen.

You are loading the scene again in RestartLevel, which causes ScoreManager to be a new object, and it will therefore set your score to what it is initialized as. Try saving the highscore in PlayerPrefs! Something like this:
PlayerPrefs.SetInt(“HighScore”, );
and then retrieving it on awake, using
PlayerPrefs.GetInt(“HighScore”);

When reloading scene everything is thrown away and brand new fresh copy is loaded from assets. Basicaly, scene reload equals to resetting everything to initial state. You need to save the value if you want it later. Commonly used solution for this are player prefs, dont destroy on load mark for game objects and standard c# static variables. Major difference is the first one saves to local storage and values are preserved between application restarts. Two others are not.

I wouldnt do this. Load your highscores on loading the game and manage them internally, store them in playerprefs (or a custom file) only when exiting the game to optimize performance. you can store them at runtime for example in a Dictionary<string, float> where string is the players name and the float is their score.

Depends entirely on the usage, but yeah, your solution is better in the long run! :slight_smile:

Yeah true, its also easier to manipulate a list and write it to a file than to edit a file directly. That is if you want to keep track of more than one high score at a time.

If only you don’t work on the connected game, where player data is stored remotely.