Creating a timer that starts when my 3rd scene is loaded
I am wanting to create a timer that will start counting down from 10 when my 3rd scene (Second Level) is loaded, then when this reaches 0 the player dies, re-spawns at the beginning of the level and then this countdown repeats itself.
I currently have that when the timer reaches 0 the player re-spawns in the correct place, but this is used using an if statement saying that if the timer is greater than or equal to 10 then to make the player position equal to the way-point, this causes problems after 10 seconds since the position is then updating every frame to that one position and my player then cannot move.
My Timer code is as below:
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour { float gameDuration = 10; bool endGame = false; private GameManager GameManager; private SetSpawn SetSpawn; private PlayerManager PlayerManager; public GameObject Player; private GameObject Waypoint; public Text timeDisplay;
// Use this for initialization
void Start () {
GameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
SetSpawn= GameObject.Find("Waypoint").GetComponent<SetSpawn>();
PlayerManager = GameObject.Find("Player").GetComponent<PlayerManager>();
}
// Update is called once per frame
void Update () {
if (Time.time >= gameDuration)
{
endGame = true;
}
if (endGame == true)
{
Player.transform.position = Player.GetComponent<PlayerManager>().spawnPoint;
GameManager.deaths = GameManager.deaths++;
print("Respawn");
}
else
{
float timeLeft = Mathf.RoundToInt(gameDuration - Time.time);
timeDisplay.text = " You have " + timeLeft.ToString() + " seconds!";
}
}
}
Answer by SarfaraazAlladin · Nov 23, 2016 at 09:58 PM
You should look into coroutines for something like this :)
Once you know how to use them, you'll find that coroutines are a great way to avoid Update, but still get frame by frame control.
In this case, however, they offer an awesome method called WaitForSeconds();
void Start ()
{
GameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
SetSpawn= GameObject.Find("Waypoint").GetComponent<SetSpawn>();
PlayerManager = GameObject.Find("Player").GetComponent<PlayerManager>();
//Start the co routine
StartCoroutine("DeathTimer");
}
IEnumerator DeathTimer()
{
//wait 10 seconds...
yield return new WaitForSeconds (10);
RespawnPlayer ();
}
void RespawnPlayer()
{
Player.transform.position = Player.GetComponent<PlayerManager>().spawnPoint;
GameManager.deaths = GameManager.deaths++;
print("Respawn");
}
And that should do it!
You'll notice that coroutines can't be called the same way as a normal method, and you'll have to use StartCoroutine(); instead.
There is a bunch to learn on the topic, but well worth your time if you google coroutines. Like I said before, you can also make a loop inside of one that will run every frame until you decide to break out of it, which is great for smaller animations or timers that would otherwise clutter up the Update loop.
Hope this helps!
EDIT
If you want to have the respawn happen in a loop, look into while loops. Here's a simple example
void Start ()
{
GameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
SetSpawn= GameObject.Find("Waypoint").GetComponent<SetSpawn>();
PlayerManager = GameObject.Find("Player").GetComponent<PlayerManager>();
//Start the co routine
StartCoroutine("DeathTimer");
}
IEnumerator DeathTimer()
{
bool stay = true;
while (stay)
{
if(/* whatever condition you want to exit while loop */)
{
stay = false;
break;
}
//wait 10 seconds...
yield return new WaitForSeconds (10);
RespawnPlayer ();
}
}
void RespawnPlayer()
{
Player.transform.position = Player.GetComponent<PlayerManager>().spawnPoint;
GameManager.deaths = GameManager.deaths++;
print("Respawn");
}
While statements are useful, but beware the infinite loop. Always make sure you have the proper logic setup to break out of your loops.
That's absolutely great thank you :D
I was hoping though that the coroutine would then restart and wait another 10 seconds before running the Respawn function again,
The level this code is running on is a timed level that you have 10 seconds to finish, if you don't then the respawn function runs, which I want it to do every 10 seconds until the player finishes the level.
Thank you for showing me coroutines though, I'm going to go research them now. ($$anonymous$$y lecturer has literally just said we're researching this now)
Thank you once again :D
I updated my answer with a second example to show how you can use a while loop to get the control you need. If you like my answer, I would be grateful if you marked it as correct :)
Have fun learning co routines