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