- Home /
Game over scene when the time is up ?
How can i do game over scene when the time is up in this code ?
using UnityEngine; using System.Collections;
public class Timer : MonoBehaviour { public float Seconds = 59; public float Minutes = 0; void Update () { if (Seconds <= 0) { Seconds = 59; if (Minutes >= 1) { Minutes--; } else { Minutes = 0; Seconds = 0; GameObject.Find ("TimerText").guiText.text = Minutes.ToString ("f0") + ":0" + Seconds.ToString ("f0"); } } else { Seconds -= Time.deltaTime; } if(Mathf.Round(Seconds) <= 9) { GameObject.Find ("TimerText").guiText.text = Minutes.ToString ("f0") + ":0" + Seconds.ToString ("f0"); } else { GameObject.Find ("TimerText").guiText.text = Minutes.ToString ("f0") + ":" + Seconds.ToString ("f0"); } } }
It's worth watching the tutorial video to learn how to format your code.
Answer by DRProductions · Jan 19, 2014 at 10:02 PM
First off make sure you format your code second make a script called Countdown or something and use this code JavaScript:
var Minutes = 1;
function Start(){
yield WaitForSeconds(Minutes * 60);
Application.LoadLevel("NAMEOFSCENE");
}
Or if you prefer C# (This may not work I don't use C# but you get the idea):
public class Example : MonoBehaviour {
public int Minutes = 1;
void Start() {
yield return new WaitForSeconds(Minutes * 60);
}
}
Are you sure you applied my script? it should work correctly
Answer by Lasent · Jan 20, 2014 at 01:50 AM
Unfortunately, in C#, Coroutines aren't that easy to start as in US. Here's how it should look: public int minutes = 1;
IEnumerator GameOver()
{
yield return new WaitForSeconds(minutes * 60);
Application.LoadLevel("GAMEOVERSCENE");
}
...
void Start () //Countdown will begin when you load in the script
{
StartCoroutine(GameOver());
...
}
In C#, you explicitly have to set the method IEnumerator, and start it with StartCoroutine.