Saving and displaying best time per level.

Hello there, so I am working on a platformer/time-attack game that has the player trying to set and beat their best time. My timer works well, but I am trouble getting to save my best time for each level and getting it to display properly.
If it’s not too much to ask, could someone eyeball my code and give me a few pointers as to what I am doing wrong? Thanks in advance!

Here’s my code:

#pragma strict
public static var startTime : float;
public static var endTime: float;
public var currentTimeLvel1: float = 0.0f;
//public var currentTimeLvel2: float = 0.0f;
//public var currentTimeLvel3: float = 0.0f;
//public var currentTimeLvel4: float = 0.0f;
//public var currentTimeLvel5: float = 0.0f;
public var onLevel1 : boolean = true;
//public var onLevel2 : bool = false;
//public var onLevel3 : bool = false;
//public var onLevel4 : bool = false;
//public var onLevel5 : bool = false;

private var bestTime : float = 0.0f ;
public var level;
var playTime = endTime - startTime;
public var textTime : String;


public var guiSkin : GUISkin;



function Start() 
{
   startTime = Time.time;
   bestTime = PlayerPrefs.GetFloat("Best Time:", Mathf.Infinity) ;
	Debug.Log ("Best Time:" + bestTime);
}
		 function Update()
	 {
	 	if(onLevel1)
	 	{
	 		SetBestTime() ;
	 	}
	 
	    
	 }
	 
	 	 function SetBestTime()
	 {
	     currentTimeLvel1 = Time.time;
	     Debug.Log ("End Time:" + currentTimeLvel1);
	     
		            if(currentTimeLvel1 < bestTime)
	       {
	          PlayerPrefs.SetFloat("Best Time:", endTime);
	          bestTime = endTime;
	       }
	 }
	 

	 
	function OnGUI () 
		{
			
			var guiTime = Time.time - startTime; 

			var minutes : int = guiTime / 60;
			var seconds : int = guiTime % 60;
			var fraction : int = (guiTime * 100) % 100;
			 
		 	textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 

			 GUI.skin = guiSkin;
			 GUI.Label(Rect((Screen.width / 2) - 100, 30, 350, 80), "TIME ELAPSED: " + textTime);
			 GUI.Label(Rect((Screen.width / 2) - 100, 60, 350, 80), "BEST TIME: " + bestTime);
		 
		}
		
			function LevelEnded()
		{
		  
		   PlayerPrefs.SetFloat("Best Time:", bestTime); 
		   Debug.Log ("Best Time:" + bestTime);
		   PlayerPrefs.Save();
		  
		}

Hey there!

Try creating 2 floats. First one is for the current time of starting the scene (the time follows itself through every scene) and then make a static float. at the end of the scene (or completion) stop the static float from being equal to the time and then deduct the starting time.

We use a static float for it so that it is not forgotten and so you can reference it in other scripts. make a save script and choose to save it in that. You can then decide to round to round it up or down and change it to minutes and everything like that. Hope this helps!

Got questions? Ask away!

Here’s a sample to make it easier to understand:

//Variables
public float currentTime;
public static float endTime;

//Check if complete (must put something in to confirm it)
public bool done = false;

//Start of the scene
void Start() {
  currentTime = Time.time
}

void Update () {
  if(done == false) {
      //Update the timer
      endTime = Time.time;
  } else if(done == true) {
      //Calculate the amount of time it took to complete the objective (Saved Variable)
      endTime = endTime - currentTime;
  }
}