- Home /
Reload level after timer hits 0
Hi,
First of all, thanks for any answer that I might (or might not!) get in this thread. I've recently started using Unity and completed the "roll a ball" tutorial. I've added a basic timer script that will count from 30 to 0 and reload the level when 0 will hit.
I'd like to add a basic "You lost!" text and reload the level after 5 seconds (or so), but I can't seem to be able to correctly add the Coroutine and the subsequent WaitForSeconds inside an "if timer hits 0 then...".
Below is my very basic code for the timer:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class myTimer : MonoBehaviour {
public float myTimer = 30;
public Text timerText;
// Use this for initialization
void Start () {
timerText = GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
myTimer -= Time.deltaTime;
timerText.text = myTimer.ToString ("f0");
print (myTimer);
if (myTimer <= 0) {
SceneManager.LoadScene (0);
}
}
}
Thank you all for your help!
Sorry, I should have been more specific. At the moment, the code does not have a couroutine called, because I did put it here:
void Start () {
timerText = GetComponent<Text> ();
StartCoroutine ("waitFiveSeconds");
}
but the problem is that I don't know where to put the WaitForSeconds bit as, if I leave it inside the "if" part, it won't work:
void Update () {
myCoolTimer -= Time.deltaTime;
timerText.text = myCoolTimer.ToString ("f0");
print (myCoolTimer);
if (myCoolTimer <= 0) {
print (youLost);
IEnumerator waitFiveSeconds () {
yield return new WaitForSeconds (5);
Scene$$anonymous$$anager.LoadScene (0);
}
Thanks!
Also, sorry if some part of the code are a bit of a mismatch, but I am trying a billion combination to make this thing work :)
you're not a million miles away...
first of all, take the coroutine out of Update()
and don't start it from Start()
!
the code will work if you start it when the player has lost so, in the original, replace your load scene call with the StartCoroutine()
call and all should be well.
you might consider making the wait time a parameter so that the code can be reused, although for now (to get it working) this will obviously be fine ;)
Answer by zeldax54 · Dec 23, 2015 at 04:11 AM
May be this:
public float myTimer = 30;
public Text timerText;
public float showsecond=5f;
bool _isshow;
// Use this for initialization
void Start () {
timerText = GetComponent<Text> ();
}
void Update () {
myTimer -= Time.deltaTime;
timerText.text = myTimer.ToString ("f0");
print (myTimer);
if(mytTimer<=showsecond && _isshow)
{
timerText.text="LOST LOST LOST";
_isshow=false;
}
if (myTimer <= 0)
SceneManager.LoadScene (0);
}
Thank you for the above script. I've tried it and it seems to work fine; however, it does not display the "You lost" text and it just reload the level as soon as the timer hits 0. :(
Thanks again for your help!
oh am sorry change this. $$anonymous$$y eeror is managing the bool variable.
if(mytTimer<=showsecond && !_isshow)
{
timerText.text="LOST LOST LOST";
_isshow=true;
}
Your answer
Follow this Question
Related Questions
How to make reverse countdown timer in unity? 1 Answer
Speed up timer 1 Answer
How do you make a countdown timer that will lose time if you press a button? 1 Answer
Stop and Pause Timer 2 Answers
timer not ticking down 2 Answers