Working on a timer for my game, but having trouble getting the game to reset once the time runs out. I’ve tried looking through several other pages of people having this problem, but I can’t seem to get it to work ><
The code I’m using:
using UnityEngine;
using System.Collections;
public class TimerScript : MonoBehaviour {
private float startTime = 0;
private float restSeconds = 0;
private int roundedRestSeconds = 0;
private float displaySeconds = 0;
private float displayMinutes = 0;
public int CountDownSeconds = 120;
private float Timeleft = 0;
string timetext = "";
void Start () {
startTime = Time.deltaTime;
}
void ResetTimer()
{
startTime = Time.time;
}
void OnGUI ()
{
startTime = 0;
Timeleft = Time.time-startTime;
restSeconds = CountDownSeconds-(Timeleft);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = (roundedRestSeconds / 60)%60;
timetext = (displayMinutes.ToString() + ":");
if (displaySeconds > 9){
timetext = timetext + displaySeconds.ToString();
}
else {
timetext = timetext + "0" + displaySeconds.ToString();
}
if (displaySeconds < 0){
ResetTimer();
Application.LoadLevel("over");
}
GUI.Box(new Rect(650.0f, 0.0f, 100.0f, 75.0f), timetext);
}
}
Any help would be greatly appreciated.