Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

highscore resets

Discussion in 'Scripting' started by ManAmazin, Nov 13, 2013.

  1. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    hey guys on i have the latest highscore displayed on main menu

    and everytime the game end or stage is complete i use this code to check against highscore in playerprefs


    Code (csharp):
    1.  
    2.  
    3. Int latestScore = scoring.score;
    4. Int prevousScore = Playerprefs.GetInt("HighScore");
    5.  
    6. if(latestScore >= previousScore)
    7. {
    8.     Playerprefs.SetInt("Highscore",latestScore);
    9. }
    10.  
    Now the problem is that for some reason, no matter the score it updates that value so if player scored 2500 first game then played again and only scored 1000 then that 1000 will overwrite the 2500 any ideas why this is? am i missing something?
     
  2. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    Code (csharp):
    1.  
    2. Int latestScore = scoring.score;
    3. Int prevousScore = Playerprefs.GetInt("HighScore");
    4.                                         // ^ Upper case 'S'
    5. if (latestScore >= previousScore)
    6. {
    7.     Playerprefs.SetInt("Highscore", latestScore);
    8.                          // ^ Lower case 's'
    9. }
     
  3. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    i actually have that in my code i just wrote this post by hand instead of copying and pasting so that was an error on my part.
     
  4. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    anyone have an idea or maybe a better way to set highscore?
     
  5. icreate

    icreate

    Joined:
    Jul 13, 2012
    Posts:
    25
    It is hard to tell without seeing the rest of the script.
    Put a break point at the "if" condition and see if the previous has the right value.

    You may also try calling PlayerPrefs.Save.
     
  6. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246


    i figured it out and yes you were right i was playing around with the playerprefs and i wasnt calling the .Save() like i should have thanks alot!
     
unityunity