- Home /
 
HighScore not updating when the game time is up...
Hi. I have two different scripts in the game, one for the countdown time (Timer) and one for the score (Score). And my point is to set a new HighScore when the time is up. However, the highscore doesn't seem to be working when I call it on debug.log, it always stays as 0. What am I doing wrong? Here is part of the Score script :
 static var score : int = 00;
 static var highscore : int = 00;
 static var highScoreKey = "HighScore";
 
 function Start () {
 
 highscore = PlayerPrefs.GetInt(highScoreKey,0);
 }
 
 function SetNewHighScore () {
     if (Timer.seconds==0) {
         if (score > highscore) {
             PlayerPrefs.SetInt(highScoreKey, score);
             PlayerPrefs.Save();
         }
     }
 }
 
               And here is part of the Timer script where I call the saved data:
 function OnGUI () {
 
 textTime = String.Format("{00:00}", seconds);
 
     GUI.skin = theTimerSkin;
     GUI.Label (new Rect (Screen.width/2,18,100,100), textTime);
 
     if(seconds <= 0) {
     seconds=0;
     **Debug.Log(Score.highscore);**
     }
 }
 
              @Tanshaydar: I use static values so I can access them from other parts. @GameVortex: Could you please develop on why I can't call it that way, I thought we could call a function whatever we want...
Answer by rutter · Sep 06, 2014 at 05:47 AM
This bit:
     if (score > highscore) {
         PlayerPrefs.SetInt(highScoreKey, score);
         PlayerPrefs.Save();
     }
 
               You're updating the high score that's saved on disk, but you're not updating the variable highscore.
You need:
     if (score > highscore) {
         highscore = score;
         PlayerPrefs.SetInt(highScoreKey, score);
         PlayerPrefs.Save();
     }
 
               You're not showing anything that calls SetNewHighScore; are you sure that's being called? 
@Rutter: Genius! Thank you very much, this solved the problem. And, yes I wasn't calling the function earlier but I started calling the function when the time is up and now it's working fine!
Your answer
 
             Follow this Question
Related Questions
how to find time of a button pressed 2 Answers
Timer help please 1 Answer
How do you save timer with PlayerPrefs? 1 Answer
How can I use time to reset a switch statement? 1 Answer
Time formatting 3 Answers