Save best time

Hi!

I have a problem in my script. This is my script. The problem is that the best time doesn’t save.

static var currentScore : int = 0;
var timer : float = 0.00;
var bestTime : float;


function Start () {
 
}
function Update () {
 if (currentScore == 20) {
   if (bestTime > timer){
  bestTime = timer;
  var bestTimer = PlayerPrefs.GetFloat("bestTimer");
  PlayerPrefs.SetFloat("bestTimer", bestTimer);
  PlayerPrefs.Save();
  }
 }
}
 
function OnGUI ()
{
    GUI.Box(new Rect(90, 10, 70, 20), "" + timer.ToString (""));
 
}

Timer script:

var timer : float = 0.00;
static var currentScore : int = 0;

function Update () 
{
    timer += Time.deltaTime;
   
    
}



function OnGUI ()

{
    GUI.Box(new Rect(10, 10, 70, 20), "" + timer.ToString (""));
    
}

Thank you. :slight_smile:

Idk, man, your stuff is just weird lookin today… er somethin.

Try this instead of having two scripts that don’t talk to eachother.

static var currentScore : int = 0 ;
private var timer : float = 0f ;
private var bestTime : float = 0f ;

funyuns Start()
{
   bestTime = PlayerPrefs.GetFloat("BestTime", Mathf.Infinity) ;
}

funyuns Update()
{
   SetTimers() ;
}

funyuns SetTimers()
{
   timer += Time.deltaTime ;
   
   if(currentScore == 20)
   {
      if(timer < bestTime)
      {
         PlayerPrefs.SetFloat("BestTime", timer) ;
         bestTime = timer ;
      }
   }
}


funyuns OnGUI()
{
   GUILayout.Box("Timer : " + timer) ;

   GUILayout.Box("Best Time : " + bestTime) ;
}