- Home /
 
Best time and saving best time
Hi!
I've made a timer in my game. This is the script of it. But I want to save best time. When a player reaches currentScore 20 I want timer to stop and then save the time if it is the best and I want it to be in GUI Box.
 var timer : float = 0.00;
 function Update () 
 {
     timer += Time.deltaTime;
 }
  
 function OnGUI ()
 {
     GUI.Box(new Rect(10, 10, 70, 20), "" + timer.ToString (""));
  
 }
 
               I was trying to do something but it doesn't work. Please understand I'm a beginner. I tried to do something logical. But it says that it is expecting ), found = and unexpected token: 20.
 static var currentScore : int = 0;
 var timer : float = 0.00;
 var bestTime = 0;
  
 function Start () {
  
 }
 function Update () {
  if (currentScore = 20) {
    if (bestTime > timer){
   bestTime = timer;
   PlayerPrefs.Save();
   }
  }
 }
  
 function OnGUI ()
 {
     GUI.Box(new Rect(90, 10, 70, 20), "" + timer) ;
  
 }
  
 
               Please help. Thank you.
Answer by robertbu · Jul 17, 2014 at 04:52 PM
The syntax error is because comparisons use double equals. So line 9 should be:
  if (currentScore == 20) {
 
               For future posts, please include a copy of the error message from the Console. It gives us the line number and the stack trace.
Thank you, now it works :). But now I have another problem best time doesn't save.
Answer by yagizpoyraz · Jul 17, 2014 at 05:15 PM
For save:
 PlayerPrefs.SetFloat("bestTimer", bestTimer);
 PlayerPrefs.Save();
 
               For read:
 var bestTimer = PlayerPrefs.GetFloat("bestTimer")
 
              You mean like this? Because save still doesn't work. :/
 static var currentScore : int = 0;
 var timer : float = 0.00;
 var bestTime = 0;
 var bestTimer = GUI.Box;
  
 function Start () {
  
 }
 function Update () {
  if (currentScore == 20) {
    if (bestTime > timer){
   bestTime = timer;
   PlayerPrefs.Save();
   }
  }
 }
  
 function OnGUI ()
 {
     GUI.Box(new Rect(90, 10, 70, 20), "" + timer) ;
  
 }
                 For save:
 PlayerPrefs.SetFloat("bestTimer", bestTimer);
 PlayerPrefs.Save();
 
                  For read:
 var bestTimer = PlayerPrefs.GetFloat("bestTimer");
                 take a look at **this** video, it explains a bit UI stuff too. OnGUI is old and slow, don't touch it for in-game purposes.
Answer by 265lutab · Feb 16, 2016 at 01:14 PM
like this
 static var currentScore : int = 0;
  var timer : float = 0.00;
  var bestTime = 0;
  var bestTimer = GUI.Box;
   
  function Start () {
   
  }
  function Update () {
   if (currentScore == 20) {
     if (bestTime > timer){
    bestTime = timer;
     PlayerPrefs.SetFloat("bestTimer", bestTimer);
  PlayerPrefs.Save();
    }
   }
  }
   
  function OnGUI ()
  {
      GUI.Box(new Rect(90, 10, 70, 20), "" + timer) ;
   
  }
 
 
              Your answer
 
             Follow this Question
Related Questions
How to set the best time and save it? 1 Answer
How to make a timer read to the .001 of a second 2 Answers
Timer Countdown fix. 2 Answers
timer that can be reset 1 Answer
How to work a real life timer? 2 Answers