I want to save a high score per scene

I know this has been asked a bunch, but I have tried a multitude of answers to no success

I was hoping to save a high score per scene. It’s a flappy bird style game… endless scoring thing

I have 2 scenes, more eventually that I want to save the score independently per scene

This is my score saving code here (Also, I am like VERY beginner C#, like seriously, so please take that into account)

   void OnPlayerDied()
    {
        gameOver = true;
        int savedScore = PlayerPrefs.GetInt("HighScore");
        if (score > savedScore)
        {
            PlayerPrefs.SetInt("HighScore", score);
        }
        SetPageState(PageState.GameOver);
    }

and this is the whole GameManager

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


public class GameManager : MonoBehaviour
{
    public delegate void GameDelegate();
    public static event GameDelegate OnGameStarted;
    public static event GameDelegate OnGameOverConfirmed;

    public static GameManager Instance;

    public GameObject startPage;
    public GameObject gameOverPage;
    public GameObject countdownPage;
    public Text scoreText;

    enum PageState
    {
        None,
        Start,
        GameOver,
        Countdown
    }

    int score = 0;
    bool gameOver = true;

    public bool GameOver { get { return gameOver; } }
    public int Score { get { return score; } }

    void Awake()
    {
        Instance = this;
    }

    void OnEnable()
    {
        CountdownText.OnCountdownFinished += OnCountdownFinished;
        TapController.OnPlayerDied += OnPlayerDied;
        TapController.OnPlayerScored += OnPlayerScored;
    }

    void OnDisable()
    {
        CountdownText.OnCountdownFinished -= OnCountdownFinished;
        TapController.OnPlayerDied -= OnPlayerDied;
        TapController.OnPlayerScored -= OnPlayerScored;
    }

    void OnCountdownFinished()
    {
        SetPageState(PageState.None);
        OnGameStarted();
        score = 0;
        gameOver = false;
    }

    void OnPlayerDied()
    {
        gameOver = true;
        int savedScore = PlayerPrefs.GetInt("HighScore");
        if (score > savedScore)
        {
            PlayerPrefs.SetInt("HighScore", score);
        }
        SetPageState(PageState.GameOver);
    }

    void OnPlayerScored()
    {
        score++;
        scoreText.text = score.ToString();
    }

    void SetPageState(PageState state)
    {
        switch (state)
        {
            case PageState.None:
                startPage.SetActive(false);
                gameOverPage.SetActive(false);
                countdownPage.SetActive(false);
                break;
            case PageState.Start:
                startPage.SetActive(true);
                gameOverPage.SetActive(false);
                countdownPage.SetActive(false);
                break;
            case PageState.GameOver:
                startPage.SetActive(false);
                gameOverPage.SetActive(true);
                countdownPage.SetActive(false);
                break;
            case PageState.Countdown:
                startPage.SetActive(false);
                gameOverPage.SetActive(false);
                countdownPage.SetActive(true);
                break;
        }
    }

    public void ConfirmGameOver()
    {
        //activated when replay buttron is hit
        OnGameOverConfirmed(); //event sent to TapController
        scoreText.text = "0";
        SetPageState(PageState.Start);
    }

    public void StartGame()
    {
        //activated when play button is hit
        SetPageState(PageState.Countdown);
    }
}

To save something in PlayerPrefs individually you will need two different instances, or else it will rewrite the previous scene’s score! Here I’ve posted your script with a change, I’ve added a variable that allow you to use the same script without the need to edit it. Only change the ‘sceneID’ to the level id. For example: if it was level 1 you should change the sceneID to 1. Hope it helped! :slight_smile:

PlayerPrefs.SetInt("HighScore1", score); //For scene 1
PlayerPrefs.SetInt("HighScore2", score; //For scene 2

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class GameManager : MonoBehaviour
 {
     public delegate void GameDelegate();
     public static event GameDelegate OnGameStarted;
     public static event GameDelegate OnGameOverConfirmed;
 
     public static GameManager Instance;
 
     public GameObject startPage;
     public GameObject gameOverPage;
     public GameObject countdownPage;
     public Text scoreText;
     public Int sceneID;
 
     enum PageState
     {
         None,
         Start,
         GameOver,
         Countdown
     }
 
     int score = 0;
     bool gameOver = true;
 
     public bool GameOver { get { return gameOver; } }
     public int Score { get { return score; } }
 
     void Awake()
     {
         Instance = this;
     }
 
     void OnEnable()
     {
         CountdownText.OnCountdownFinished += OnCountdownFinished;
         TapController.OnPlayerDied += OnPlayerDied;
         TapController.OnPlayerScored += OnPlayerScored;
     }
 
     void OnDisable()
     {
         CountdownText.OnCountdownFinished -= OnCountdownFinished;
         TapController.OnPlayerDied -= OnPlayerDied;
         TapController.OnPlayerScored -= OnPlayerScored;
     }
 
     void OnCountdownFinished()
     {
         SetPageState(PageState.None);
         OnGameStarted();
         score = 0;
         gameOver = false;
     }
 
     void OnPlayerDied()
     {
         gameOver = true;
         int savedScore = PlayerPrefs.GetInt("HighScore" + sceneID);
         if (score > savedScore)
         {
             PlayerPrefs.SetInt("HighScore" + sceneID, score);
         }
         SetPageState(PageState.GameOver);
     }
 
     void OnPlayerScored()
     {
         score++;
         scoreText.text = score.ToString();
     }
 
     void SetPageState(PageState state)
     {
         switch (state)
         {
             case PageState.None:
                 startPage.SetActive(false);
                 gameOverPage.SetActive(false);
                 countdownPage.SetActive(false);
                 break;
             case PageState.Start:
                 startPage.SetActive(true);
                 gameOverPage.SetActive(false);
                 countdownPage.SetActive(false);
                 break;
             case PageState.GameOver:
                 startPage.SetActive(false);
                 gameOverPage.SetActive(true);
                 countdownPage.SetActive(false);
                 break;
             case PageState.Countdown:
                 startPage.SetActive(false);
                 gameOverPage.SetActive(false);
                 countdownPage.SetActive(true);
                 break;
         }
     }
 
     public void ConfirmGameOver()
     {
         //activated when replay buttron is hit
         OnGameOverConfirmed(); //event sent to TapController
         scoreText.text = "0";
         SetPageState(PageState.Start);
     }
 
     public void StartGame()
     {
         //activated when play button is hit
         SetPageState(PageState.Countdown);
     }
 }