There are many ways to wait in Unity. It's really simple, but I think it's worth paying attention to most ways to do this:
1 . With coroutine and WaitForSeconds .
This is by far the easiest way. Put all the code that you need to wait for some time in the coroutine function, you can wait with WaitForSeconds . Note that in the coroutine function, you call the function using StartCoroutine(yourFunction) .
The example below will rotate 90 degrees, wait 4 seconds, rotate 40 degrees and wait 2 seconds, and then finally rotate the rotation 20 degrees
void Start() { StartCoroutine(waiter()); } IEnumerator waiter() { //Rotate 90 deg transform.Rotate(new Vector3(90, 0, 0), Space.World); //Wait for 4 seconds yield return new WaitForSeconds(4); //Rotate 40 deg transform.Rotate(new Vector3(40, 0, 0), Space.World); //Wait for 2 seconds yield return new WaitForSeconds(2); //Rotate 20 deg transform.Rotate(new Vector3(20, 0, 0), Space.World); }
2 . With coroutine and WaitForSecondsRealtime .
The only difference between WaitForSeconds and WaitForSecondsRealtime is that WaitForSecondsRealtime uses unscaled time to wait, which means that when you pause the game with Time.timeScale , the WaitForSecondsRealtime function will not be affected, and WaitForSeconds will be.
void Start() { StartCoroutine(waiter()); } IEnumerator waiter() { //Rotate 90 deg transform.Rotate(new Vector3(90, 0, 0), Space.World); //Wait for 4 seconds yield return new WaitForSecondsRealtime(4); //Rotate 40 deg transform.Rotate(new Vector3(40, 0, 0), Space.World); //Wait for 2 seconds yield return new WaitForSecondsRealtime(2); //Rotate 20 deg transform.Rotate(new Vector3(20, 0, 0), Space.World); }
Wait and you can see how long you waited:
3 . With coroutine and variable increment each frame with Time.deltaTime .
A good example of this is when you need a timer to display on the screen how long it has been waiting. Basically, like a timer.
It is also good when you want to abort wait / sleep with the boolean variable when it is true. Here you can use yield break; .
bool quit = false; void Start() { StartCoroutine(waiter()); } IEnumerator waiter() { float counter = 0; //Rotate 90 deg transform.Rotate(new Vector3(90, 0, 0), Space.World); //Wait for 4 seconds float waitTime = 4; while (counter < waitTime) { //Increment Timer until counter >= waitTime counter += Time.deltaTime; Debug.Log("We have waited for: " + counter + " seconds"); //Wait for a frame so that Unity doesn't freeze //Check if we want to quit this function if (quit) { //Quit function yield break; } yield return null; } //Rotate 40 deg transform.Rotate(new Vector3(40, 0, 0), Space.World); //Wait for 2 seconds waitTime = 2; //Reset counter counter = 0; while (counter < waitTime) { //Increment Timer until counter >= waitTime counter += Time.deltaTime; Debug.Log("We have waited for: " + counter + " seconds"); //Check if we want to quit this function if (quit) { //Quit function yield break; } //Wait for a frame so that Unity doesn't freeze yield return null; } //Rotate 20 deg transform.Rotate(new Vector3(20, 0, 0), Space.World); }
You can simplify this by moving the while to another co-programming function and yielding to it, and you can still see how it counts and even breaks the counter.
bool quit = false; void Start() { StartCoroutine(waiter()); } IEnumerator waiter() { //Rotate 90 deg transform.Rotate(new Vector3(90, 0, 0), Space.World); //Wait for 4 seconds float waitTime = 4; yield return wait(waitTime); //Rotate 40 deg transform.Rotate(new Vector3(40, 0, 0), Space.World); //Wait for 2 seconds waitTime = 2; yield return wait(waitTime); //Rotate 20 deg transform.Rotate(new Vector3(20, 0, 0), Space.World); } IEnumerator wait(float waitTime) { float counter = 0; while (counter < waitTime) { //Increment Timer until counter >= waitTime counter += Time.deltaTime; Debug.Log("We have waited for: " + counter + " seconds"); if (quit) { //Quit function yield break; } //Wait for a frame so that Unity doesn't freeze yield return null; } }
Wait / hibernate until the variable changes or is equal to another value :
4 . With coroutine and WaitUntil function:
Wait until the condition becomes true . For example, a function that expects a player score to be 100 , then loads the next level.
float playerScore = 0; int nextScene = 0; void Start() { StartCoroutine(sceneLoader()); } IEnumerator sceneLoader() { Debug.Log("Waiting for Player score to be >=100 "); yield return new WaitUntil(() => playerScore >= 10); Debug.Log("Player score is >=100. Loading next Leve");
5 . With coroutine and WaitWhile function.
Wait for the condition to be true . For example, when you want to exit the application when the evacuation key is pressed.
void Start() { StartCoroutine(inputWaiter()); } IEnumerator inputWaiter() { Debug.Log("Waiting for the Exit button to be pressed"); yield return new WaitWhile(() => !Input.GetKeyDown(KeyCode.Escape)); Debug.Log("Exit button has been pressed. Leaving Application"); //Exit program Quit(); } void Quit() {
6 . Using the Invoke Function:
You can call Unity to call a function in the future. When you call the Invoke function, you can pass the time-out before calling this function to the second parameter. The example below will call the feedDog() function after 5 seconds, on which Invoke is called.
void Start() { Invoke("feedDog", 5); Debug.Log("Will feed dog after 5 seconds"); } void feedDog() { Debug.Log("Now feeding Dog"); }
7 . Using the Update() and Time.deltaTime .
It is just like # 3 , except that it does not use coroutines. It uses the Update function.
The problem is that this requires so many variables that it does not start every time, but only once, when the timer ends after waiting.
float timer = 0; bool timerReached = false; void Update() { if (!timerReached) timer += Time.deltaTime; if (!timerReached && timer > 5) { Debug.Log("Done waiting"); feedDog();
There are still other ways to wait in Unity, but you should definitely know the ones mentioned above, as this makes it easier to create games in Unity. When to use each of them depends on the circumstances.
For your specific problem, this is the solution:
IEnumerator showTextFuntion() { TextUI.text = "Welcome to Number Wizard!"; yield return new WaitForSeconds(3f); TextUI.text = ("The highest number you can pick is " + max); yield return new WaitForSeconds(3f); TextUI.text = ("The lowest number you can pick is " + min); }
And to call / run the coroutine function from your start or update function, you call it with
StartCoroutine (showTextFuntion());