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
    using UnityEngine;
    using UnityEngine.UI;

    public class ScoreManager : MonoBehaviour
    {

    ///<summary> The transform ofour player, for determining position. </summary>
    [Header("Please Drag Objects Here")]
    public Transform player;
    ///<summary> The UI element to display our current score. </summary>
    public Text currentScoreText;
    ///<summary> The UI element to display our high score. </summary>
    public Text highScoreText;

    // Good idea to keep strings like this in a field, to avoid typos later.
    private const string highScoreKey = "HighScore";

    // Hold our score values.
    [Header("Do not change values")]
    [SerializeField] private int highScore = 0;
    [SerializeField] private int currentScore = 0;

    ///<summary>
    /// Get our current high score if we have it and dipslay it in the UI.
    ///</summary>
    private void start()
    {
    // TODO: The high score could be retrieved in Awake()

    // Get the current high score from player prefs, 0 if id doesn't exist.
    highScore = PlayerPrefs.GetInt(highScoreKey, 0);

    // Set the UI text to the current high score.
    highScoreText.text = highScore.ToString();
    }

    ///<summary>
    /// Calculate our current score based on z position. Display current score to UI.
    /// Check if current score is greater than high score. If so, update appropriately.
    ///</summary>
    private void Update()
    {
    // Calculate the current score based on player positon, round to int.
    currentScore = Mathf.RoundToInt(player.position.z);
    // Display current score in the UI.
    currentScoreText.text = currentScore.ToString();

    // Check if our new score is greater than our previous high score.
    if (currentScore > highScore)
    {
    // Change the high score to the new current value.
    highScore = currentScore;
    // Update the high score UI text.
    highScoreText.text = highScore.ToString();

    // TODO: Delete me, unless for some odd reason I am needed... But I should not be :D
    PlayerPrefs.SetInt(highScoreKey, highScore);
    PlayerPrefs.Save();
    }
    }

    ///<summary>
    /// When this gameobject is disabled, save our players highscore.
    ///</summary?
    private void OnDisable()
    {
    /*
    * OnDisable() is called anytime this object is disabled. This
    * includes changing scenes and quitting the application.
    */

    // Set our high score.
    PlayerPrefs.SetInt(highScoreKey, highScore);
    // Save our data.
    PlayerPrefs.Save();

    // TODO: Delete me, for debugging only!
    Debug.Log("I'm being Disabled! The high score is currently: " + highScore);
    }

    private bool SaveHighScore(int newScore)
    {
    int highScore = PlayerPrefs.GetInt("HighScore", 0);
    bool gotNewHighScore = newScore > highScore;

    if (gotNewHighScore)
    {
    PlayerPrefs.SetInt("HighScore", newScore);
    PlayerPrefs.Save();
    }

    return gotNewHighScore;
    }
    }


    Btw i used this script from someone else because I've spent 4 days trying to make one and just cant figure it out so help would be much help so idk what it means by TO DO
     
    Last edited: Apr 18, 2021
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Use code tags when posting code.
    Sorry, I'm just not going to try to read this wall of code.
     
  3. 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. }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    First thing I notice is start is not right. Methods should always begin with a capital letter and especially when you want Unity to run it's "magic methods"

    Change start to Start and see if that helps.
     
  5. allblakes19

    allblakes19

    Joined:
    Mar 11, 2021
    Posts:
    5
    Thank you very much it is working now