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. Dismiss Notice

Score and Highscore script

Discussion in 'Scripting' started by Venatal, Jan 14, 2016.

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    For some reason with my code below if I get a score greater than my previous highscore it does not show as New highscore: X but just as highscore: X.
    Code (CSharp):
    1.  
    2. public void  GameOverScore()
    3.     {
    4.  
    5.         if (Score > PlayerPrefs.GetInt("Highscore"))
    6.         {
    7.             PlayerPrefs.SetInt("Highscore", Score);
    8.             HighScoreScript.manager.newHighScore();
    9.             ScoreScript.manager.LatestScore();
    10.         }
    11.  
    12.         if (Score <= PlayerPrefs.GetInt("Highscore"))
    13.         {
    14.             PlayerPrefs.SetInt("LatestScore", Score);
    15.             HighScoreScript.manager.highScore();
    16.             ScoreScript.manager.LatestScore();
    17.         }
    18.  
    19.     }
    20.  
    That code links with this:
    Code (CSharp):
    1.  
    2. public void newHighScore()
    3.     {
    4.         HiScore.text = "New Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
    5.     }
    6.  
    7.     public void highScore()
    8.     {
    9.         HiScore.text = "Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
    10.     }
    11.  
    Can anyone spot what's wrong?
     
    Last edited: Jan 14, 2016
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    put a debug.log entry into the newHighScore function & see if it is going to it.

    When you get a new high score & it just show High score does it show the correct (new) value?
     
  3. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    It seems to play both at the same time for some reason..
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    i think what is happening is that because you used 2 if statements it does the first one, goes to the new high score, then goes back & checks the next if statement & now the score equals the high score. Change the second if check to else if instead so it can only do one.
     
  5. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    I heard that using else if is bad though ?
    But it does work, thanks a lot man.
    I can finally go sleep!:D
     
    tedthebug likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    wut? o_O:confused:
     
  7. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    It was in a video, if I can find it I'll link it.
     
  8. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Don't ever watch videos from that person again.
     
    tedthebug, Venatal and roojerry like this.