Search Unity

My score is not getting saved

Discussion in 'Scripting' started by max3241, Aug 4, 2020.

  1. max3241

    max3241

    Joined:
    Jul 12, 2016
    Posts:
    37
    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!
    Code (CSharp):
    1. public class ScoreManager : MonoBehaviour
    2. {
    3.     #region Consts
    4.     public const string HIGHEST_SCORE = "HighestScore";
    5.     #endregion
    6.     #region Data
    7.     [SerializeField] Text gameplaySessionText;
    8.     [SerializeField]  Text gameoverSessionText, gameoverHighestText;
    9.     #endregion
    10.     #region Properties
    11.     /// <summary>
    12.     /// Local Storage of Current Session Score
    13.     /// </summary>
    14.     public int SessionScore { get; private set; }
    15.     /// <summary>
    16.     /// Local Storage of Highest Game Score
    17.     /// </summary>
    18.     public int HighestScore { get; private set; }
    19.     public static ScoreManager instance { get; private set; }
    20.     #endregion
    21.     #region Unity Functions
    22.     private void Awake()
    23.     {
    24.         if (instance != this && instance != null)
    25.             DestroyImmediate(gameObject);
    26.         instance = this;
    27.         if (FindObjectsOfType(typeof(ScoreManager)).Length > 1)
    28.         {
    29.             DestroyImmediate(gameObject);
    30.         }
    31.         // Loading in Highest Score for consistency
    32.         HighestScore = PlayerPrefs.GetInt(HIGHEST_SCORE);
    33.         // Updating Score Display
    34.         UpdateScoreText();
    35.     }
    36.     private void Start()
    37.     {
    38.         DontDestroyOnLoad(gameObject);
    39.     }
    40.     #endregion
    41.     #region Public Functions
    42.     public void AddScore(int amount)
    43.     {
    44.         // Adding Score to Current Total
    45.         SessionScore += amount;
    46.         // Checking if Current Score is better than Highest Score
    47.         if (SessionScore > HighestScore)
    48.         {
    49.             // Updating Highest Score
    50.             HighestScore = SessionScore;
    51.             PlayerPrefs.SetInt(HIGHEST_SCORE, HighestScore);
    52.         }
    53.         // Updating Score Display
    54.         UpdateScoreText();
    55.     }
    56.     public void ResetScore()
    57.     {
    58.         SessionScore = 0;
    59.         UpdateScoreText();
    60.     }
    61.     #endregion
    62.     #region Private Functions
    63.     private void UpdateScoreText()
    64.     {
    65.         gameplaySessionText.text = SessionScore.ToString();
    66.         gameoverSessionText.text = SessionScore.ToString();
    67.         gameoverHighestText.text = HighestScore.ToString();
    68.     }
    69.     #endregion
    70. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Well.. I presume you're calling ResetScore() from somewhere? That resets the session score to 0.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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!
     
    Joe-Censored and PraetorBlue like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
     
    Last edited: Aug 5, 2020