Search code examples
c#unity-game-enginetimersave

Save Timer HighScore


I am currently working on a game were it has a timer go and the best time is the one that can complete the course the quickest. For example, the first run if I get 40 seconds that will be saved as the high score but if the second run I get 30 seconds that will be the new high score. My app currently does the opposite were if I get a higher time that will be the new high score.

Notes: Yes I have tried to switch the sign to less than '(t < PlayerPrefs.GetFloat ("HighScore", 0))' but the issue then is that no time can beat "0" as a high score.

Source Code C#:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using UnityEngine.Internal;

public class TImer : MonoBehaviour {

 public Text timerText;
 public Text highscore;
 private float startTime;
 private bool finished = false;



 void Start () {

     startTime = Time.time;
     highscore.text = PlayerPrefs.GetFloat ("HighScore", 0).ToString();
 }




 void Update ()
 {

     float t = Time.time - startTime;
     string minutes = ((int)t / 60).ToString ();
     string seconds = (t % 60).ToString ("f2");
     timerText.text = minutes + ":" + seconds;
     if (t > PlayerPrefs.GetFloat ("HighScore", 0)) {
         PlayerPrefs.SetFloat ("HighScore", t);
         highscore.text = t.ToString ();
     } 
}

Solution

  • There are a few problems.

    1st if you want to save the lowest you need to do what Peter Bons suggested and compare using float.MaxValue

    2nd you are updating the HighScore during the update, it should only be updated when the player finishes the game. What happens now is, you start the game, HighScore is set to 0 or something near 0 in the update. If you stores the lowest time as HighScore that will never work because the Player achieves the "highest" (or best) score at the start of the game.

    With that in mind I made some changes in your code. Make sure you call GameFinished() when the player reaches the end of the level/game.

    using System.Collections; 
    using System.Collections.Generic; 
    using UnityEngine; 
    using UnityEngine.UI; 
    using UnityEngine.Internal;
    
    public class TImer : MonoBehaviour {
    
     public Text timerText;
     public Text highscore;
     private float startTime;
     private bool finished = false;
    
     void GameFinished()
     {
          float t = Time.time - startTime;
          if (t < PlayerPrefs.GetFloat ("HighScore", float.MaxValue)) {
                 PlayerPrefs.SetFloat ("HighScore", t);
                 highscore.text = t.ToString ();
                 PlayerPrefs.Save();
           } 
     }
    
     void Start () {
    
         startTime = Time.time;
         highscore.text = PlayerPrefs.GetFloat ("HighScore", 0).ToString();
     }
    
    
     void Update ()
     {
    
         float t = Time.time - startTime;
         string minutes = ((int)t / 60).ToString ();
         string seconds = (t % 60).ToString ("f2");
         timerText.text = minutes + ":" + seconds;
    
         if(/* Check if player finished the game */)
               GameFinished();
    }