WaitForSeconds
Suspends the coroutine execution for the specified scaled time in seconds.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: YieldInstruction
public sealed class WaitForSeconds : YieldInstruction
Remarks
WaitForSeconds can only be used with a statement in coroutines.
yieldThe real time suspended is equal to the given time divided by Time.timeScale. To wait using unscaled time, refer to WaitForSecondsRealtime.
The actual length of time waited might not match the time specified for the following reasons:
- If you start with duration
WaitForSecondsin a long frame (for example, one which has a long operation which blocks the main thread such as some synchronous loading), the coroutine resumestseconds after the end of the frame rather thantseconds after it was called.t - The coroutine might resume on the first frame after seconds has passed rather than immediately after
tseconds has passed.t
using UnityEngine;using System.Collections;public class WaitForSecondsExample : MonoBehaviour{ void Start() { //Start the coroutine we define below named ExampleCoroutine. StartCoroutine(ExampleCoroutine()); } IEnumerator ExampleCoroutine() { //Print the time of when the function is first called. Debug.Log("Started Coroutine at timestamp : " + Time.time); //yield on a new YieldInstruction that waits for 5 seconds. yield return new WaitForSeconds(5); //After we have waited 5 seconds print the time again. Debug.Log("Finished Coroutine at timestamp : " + Time.time); }}
Additional Resources: MonoBehaviour.StartCoroutine, AsyncOperation, WaitForEndOfFrame, WaitForFixedUpdate, WaitForSecondsRealtime, WaitUntil, WaitWhile.
Constructors
Constructor | Description |
|---|---|
| WaitForSeconds(System.Single) | Suspends the coroutine execution for the given amount of seconds using scaled time. |