Search Unity

High Score is not Saving Unity

Discussion in 'Scripting' started by allblakes19, Apr 18, 2021.

  1. allblakes19

    allblakes19

    Joined:
    Mar 11, 2021
    Posts:
    5
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ScoreManager : MonoBehaviour
    5. {
    6.  
    7. ///<summary> The transform ofour player, for determining position. </summary>
    8. [Header("Please Drag Objects Here")]
    9. public Transform player;
    10. ///<summary> The UI element to display our current score. </summary>
    11. public Text currentScoreText;
    12. ///<summary> The UI element to display our high score. </summary>
    13. public Text highScoreText;
    14.  
    15. // Good idea to keep strings like this in a field, to avoid typos later.
    16. private const string highScoreKey = "HighScore";
    17.  
    18. // Hold our score values.
    19. [Header("Do not change values")]
    20. [SerializeField] private int highScore = 0;
    21. [SerializeField] private int currentScore = 0;
    22.  
    23. ///<summary>
    24. /// Get our current high score if we have it and dipslay it in the UI.
    25. ///</summary>
    26. private void start()
    27. {
    28. // TODO: The high score could be retrieved in Awake()
    29.  
    30. // Get the current high score from player prefs, 0 if id doesn't exist.
    31. highScore = PlayerPrefs.GetInt(highScoreKey, 0);
    32.  
    33. // Set the UI text to the current high score.
    34. highScoreText.text = highScore.ToString();
    35. }
    36.  
    37. ///<summary>
    38. /// Calculate our current score based on z position. Display current score to UI.
    39. /// Check if current score is greater than high score. If so, update appropriately.
    40. ///</summary>
    41. private void Update()
    42. {
    43. // Calculate the current score based on player positon, round to int.
    44. currentScore = Mathf.RoundToInt(player.position.z);
    45. // Display current score in the UI.
    46. currentScoreText.text = currentScore.ToString();
    47.  
    48. // Check if our new score is greater than our previous high score.
    49. if (currentScore > highScore)
    50. {
    51. // Change the high score to the new current value.
    52. highScore = currentScore;
    53. // Update the high score UI text.
    54. highScoreText.text = highScore.ToString();
    55.  
    56. // TODO: Delete me, unless for some odd reason I am needed... But I should not be :D
    57. PlayerPrefs.SetInt(highScoreKey, highScore);
    58. PlayerPrefs.Save();
    59. }
    60. }
    61.  
    62. ///<summary>
    63. /// When this gameobject is disabled, save our players highscore.
    64. ///</summary?
    65. private void OnDisable()
    66. {
    67. /*
    68. * OnDisable() is called anytime this object is disabled. This
    69. * includes changing scenes and quitting the application.
    70. */
    71.  
    72. // Set our high score.
    73. PlayerPrefs.SetInt(highScoreKey, highScore);
    74. // Save our data.
    75. PlayerPrefs.Save();
    76.  
    77. // TODO: Delete me, for debugging only!
    78. Debug.Log("I'm being Disabled! The high score is currently: " + highScore);
    79. }
    80.  
    81. private bool SaveHighScore(int newScore)
    82. {
    83. int highScore = PlayerPrefs.GetInt("HighScore", 0);
    84. bool gotNewHighScore = newScore > highScore;
    85.  
    86. if (gotNewHighScore)
    87. {
    88. PlayerPrefs.SetInt("HighScore", newScore);
    89. PlayerPrefs.Save();
    90. }
    91.  
    92. return gotNewHighScore;
    93. }
    94. }
    Btw I did not make this code because I tried for 4 days and couldn't do a high score so I copied someone else's so idk what it means by TODO:
     
  2. CMDR_Garbage

    CMDR_Garbage

    Joined:
    Apr 29, 2019
    Posts:
    22
    First things first, you really need to describe your problem in more detail. Posting code with a title "it doesn't work" helps no one.
    Second thing, a comment with TODO usually literally means "to do", stuff that still needs to be changed or added to the code. You also have a comment like FIXME, which as the name suggests, means that the piece of code is broken/bugged.