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
}