Hello,
I am trying to make a timer which does 3 things…
- goes up in minutes/seconds/milliseconds(00:00:00).
- save time
- display best time
I don’t know on how i go about doing this, this is closest I got…
Any help or advice will be greatly appreciated. Thanks
public Text timerText, timerTextFin, bestTimeText;
private float startTime;
private bool finished = false;
float endTimeFloat, bestTimeFloat;
public PlayerControl thePlayer;
public GameManager theGameManager;
public GameObject theTime, finishedTime, outOfTime;
void Update(){
//TIMER
float t = Time.time - startTime;
//STOPS TIMER IF PLAYER REACHES FINISH POINT
if (finished) {
return;
t = bestTimeFloat;
PlayerPrefs.SetFloat ("bestTimeFloat", bestTimeFloat);
if (t < bestTimeFloat) {
timerText = bestTimeText;
PlayerPrefs.GetFloat ("bestTimeFloat");
PlayerPrefs.Save ();
}
}
//TIMER WHICH HAS (00:00.00)
string minutes = ((int)t / 60).ToString ();
string seconds = (t % 60).ToString ("f2");
timerText.text = minutes + ":" + seconds;
timerTextFin.text = minutes + ":" + seconds;
bestTimeText.text = minutes + ":" + seconds;
//IF TIME REACHES 5mins STOPS GAMEPLAY
if (t >= 300.00) {
finished = true;
theTime.SetActive (false);
outOfTime.SetActive (true);
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player") {
finished = true;
thePlayer.enabled = false;
theTime.SetActive (false);
finishedTime.SetActive (true);
}
}
}