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.

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.

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) ;
  
 }