In my game I have a simple timer that counts down from 60 seconds. It restarts if the game is reset. Now I want to add a script that displays how much time was left after a certain scene is loaded. The script works but now if the game is restarted the new timer doesn’t restart! What am I missing?
private var seconds = 60.0;
function OnLevelWasLoaded ( level : int ) {
timeLeft = seconds - Time.time;
timeLeft = Mathf.Max (0, timeLeft);
guiText.text = FormatTime(seconds);
seconds -= Time.deltaTime;
if (level == 1) {
Destroy(gameObject);
}
if (level == 6) {
guiText.text = "Your Time Left was " + timeLeft;
}
}
function FormatTime (time) {
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
timeText = minutes.ToString () + ":";
timeText = timeText + seconds.ToString ();
return timeText;
}
I combined both scripts into one but its still not working. After the game loads scene 7 or 8 the main timer restarts when the game is reloaded but the “Your time was” part does not. Anybody??
private var seconds = 60.0;
var running = false;
DontDestroyOnLoad(this);
function Update () {
if (running) {
timeLeft = seconds - Time.time;
timeLeft = Mathf.Max (0, timeLeft);
guiText.text = FormatTime (seconds);
seconds -= Time.deltaTime;
}
if (seconds <= 0) {
Application.LoadLevel(7);
}
}
function OnLevelWasLoaded ( level : int ) {
timeLeft = seconds - Time.time;
timeLeft = Mathf.Max (0, timeLeft);
guiText.text = FormatTime(seconds);
seconds -= Time.deltaTime;
if (level == 6) {
guiText.text = "Your Time Left was " + timeLeft;
}
if (level == 7) {
Destroy(gameObject);
}
if (level == 8) {
Destroy(gameObject);
}
}
function FormatTime (time) {
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
timeText = minutes.ToString () + ":";
timeText = timeText + seconds.ToString ();
return timeText;
}