Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

High Score Keeps Reseting JS

Discussion in 'Scripting' started by Carlos2295, May 26, 2015.

  1. Carlos2295

    Carlos2295

    Joined:
    Apr 13, 2015
    Posts:
    6
    Hi there, I have a script here that upon starting the game, it displays the previous high score. When the current score exceeds the high score, it will replace it with that. However, when the game starts, the high score is there, but when a single point is scored, it automatically resets it to the current score. Any assistance would be appreciated.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import UnityEngine.UI;
    4.  
    5. var CurrentScore : float;
    6. var CurrentScoreText : Text;
    7. var HighScore : float;
    8. var HighScoreText : Text;
    9.  
    10.  
    11. function Update ()
    12. {
    13.    
    14.     var HighDisplay = PlayerPrefs.GetFloat("HighScore_Save", HighScore);
    15.     CurrentScoreText.text = "" + CurrentScore;
    16.     HighScoreText.text = "" + HighDisplay;
    17.  
    18.     if (CurrentScore > HighScore)
    19.     {
    20.         HighScore = CurrentScore;
    21.         PlayerPrefs.SetFloat("HighScore_Save", HighScore);
    22.     }
    23.    
    24. }
     
  2. legendz25

    legendz25

    Joined:
    May 30, 2014
    Posts:
    17
    Not sure about UnityScript but I"ll take a crack at it.

    Line 14 I don't think you need to use HighScore.
    Line 18 I think u need to compare CurrentScore > HighDisplay instead.

    Otherwise, everything else should work.

    Apologies if I don't get it right on the first crack.
     
  3. Xoduz

    Xoduz

    Joined:
    Apr 6, 2013
    Posts:
    135
    legendz25 is correct. Your GetFloat should look like this:
    var HighDisplay = PlayerPrefs.GetFloat("HighScore_Save");

    ...unless you want to show a default value for HighDisplay if a value hasn't been stored yet for HighScore_Save in player prefs. Then you can use the following, where you replace (or keep) 0 with the default value you want to display:
    var HighDisplay = PlayerPrefs.GetFloat("HighScore_Save", 0);

    Also, a tip: As an alternative of doing CurrentScoreText.text = "" + CurrentScore; you can instead do CurrentScoreText.text = CurrentScore.ToString();
     
  4. Carlos2295

    Carlos2295

    Joined:
    Apr 13, 2015
    Posts:
    6
    Thanks guys, I got it to work. Here's my new code. You've all been really helpful.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import UnityEngine.UI;
    4.  
    5. var CurrentScore : float;
    6. var CurrentScoreText : Text;
    7. var HighScore : float;
    8. var HighScoreText : Text;
    9.  
    10.  
    11. function Update ()
    12. {
    13.    
    14.     var HighDisplay = PlayerPrefs.GetFloat("HighScore_Save", HighScore);
    15.     CurrentScoreText.text = "" + CurrentScore;
    16.     HighScoreText.text = "" + HighDisplay;
    17.  
    18.     if (CurrentScore > HighDisplay)
    19.     {
    20.         HighScore = CurrentScore;
    21.         PlayerPrefs.SetFloat("HighScore_Save", HighScore);
    22.     }
    23.    
    24. }