My score is not getting saved

Hey guys,
I need help regarding my ScoreManager.So in my score manager, I made 3 public texts. The gameplaySessionText is the score text that appears when you play the game and the two others appears when you lose in the game. So it tells you your final score when you die. The problem is that when I play my game and finish the level and go to the next level, the gameplaySessionText isn’t saved. It goes back to 0. Can someone pls help me!

public class ScoreManager : MonoBehaviour
{
    #region Consts
    public const string HIGHEST_SCORE = "HighestScore";
    #endregion
    #region Data
    [SerializeField] Text gameplaySessionText;
    [SerializeField]  Text gameoverSessionText, gameoverHighestText;
    #endregion
    #region Properties
    /// <summary>
    /// Local Storage of Current Session Score
    /// </summary>
    public int SessionScore { get; private set; }
    /// <summary>
    /// Local Storage of Highest Game Score
    /// </summary>
    public int HighestScore { get; private set; }
    public static ScoreManager instance { get; private set; }
    #endregion
    #region Unity Functions
    private void Awake()
    {
        if (instance != this && instance != null)
            DestroyImmediate(gameObject);
        instance = this;
        if (FindObjectsOfType(typeof(ScoreManager)).Length > 1)
        {
            DestroyImmediate(gameObject);
        }
        // Loading in Highest Score for consistency
        HighestScore = PlayerPrefs.GetInt(HIGHEST_SCORE);
        // Updating Score Display
        UpdateScoreText();
    }
    private void Start()
    {
        DontDestroyOnLoad(gameObject);
    }
    #endregion
    #region Public Functions
    public void AddScore(int amount)
    {
        // Adding Score to Current Total
        SessionScore += amount;
        // Checking if Current Score is better than Highest Score
        if (SessionScore > HighestScore)
        {
            // Updating Highest Score
            HighestScore = SessionScore;
            PlayerPrefs.SetInt(HIGHEST_SCORE, HighestScore);
        }
        // Updating Score Display
        UpdateScoreText();
    }
    public void ResetScore()
    {
        SessionScore = 0;
        UpdateScoreText();
    }
    #endregion
    #region Private Functions
    private void UpdateScoreText()
    {
        gameplaySessionText.text = SessionScore.ToString();
        gameoverSessionText.text = SessionScore.ToString();
        gameoverHighestText.text = HighestScore.ToString();
    }
    #endregion
}

Well… I presume you’re calling ResetScore() from somewhere? That resets the session score to 0.

Your singleton implementation is quite problematic and could be the root of all your problems.

In Awake(), first it sees if there is one already, then it destroys this one.

Now having destroyed this one, it does not return immediately.

Instead, it sets the stored instance to this now-destroyed one. (!!!)

After that it tries to check for two or more in scene and re-destroys this one. (!!!?)

Etc.

Just google around for an already-existing Unity singleton. There’s plenty of variants, no need to roll your own and then debug it!

2 Likes

Finally got around to hammering out some simple examples here:

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!