seconds | Delay execution by the amount of time in seconds. |
Suspends the coroutine execution for the given amount of seconds using scaled time.
Create a yield
instruction. Wait for seconds
multiplied by Time.scaledTime. If seconds
is
set to 2.0f
and Time.scaledTime is set to 0.5f
, the wait is 4.0f
(2.0f
divided by 0.5f
seconds).
The example WaitForSeconds has a value of 1.0f
. The second button changes Time.scaledTime
to 4.0f
. The cubes now move faster.
using System.Collections; using System.Collections.Generic; using UnityEngine;
// WaitForSeconds example. // // Create three cubes. Press the "Move cubes normally" button to animate them. // Press the "Move cubes quickly" button to animate them faster. // Have WaitForSeconds wait for different amounts of time. The cubes // move upward or downward one at a time.
public class ScriptExample : MonoBehaviour { private GameObject[] gameObjects; private int numberOfGameObjects = 3; private bool exampleInUse = false; private int width, height; private float xPos, yPos; private float xSize, ySize; private int fontSize;
void Awake() { gameObjects = new GameObject[numberOfGameObjects];
for (int i = 0; i < numberOfGameObjects; i++) { // Create a cube, place in the world, and set the name. gameObjects[i] = GameObject.CreatePrimitive(PrimitiveType.Cube); gameObjects[i].transform.position = new Vector3(-1.5f + i * 1.5f, 0.5f, 0.0f); gameObjects[i].name = i.ToString(); }
// The size and position of the buttons. width = Screen.width; height = Screen.height; xPos = width - width / 2.5f; yPos = height - height / 5; xSize = 250 * width / 800; ySize = 40 * height / 600; fontSize = 24 * width / 800;
// Place the camera in a good location and looking towards the cubes. Camera.main.transform.position = new Vector3(2.6f, 2.0f, 2.5f); Camera.main.transform.localEulerAngles = new Vector3(25.0f, -135.0f, 0.0f); }
// Move selected cube up or down. void ChangeSize(int gameObject, float x) { if (gameObjects[gameObject].transform.position.y > 0.95f) { gameObjects[gameObject].transform.position = new Vector3(x, 0.5f, 0.0f); } else { gameObjects[gameObject].transform.position = new Vector3(x, 1.0f, 0.0f); } }
IEnumerator Example() { // Start control of the three cubes. exampleInUse = true;
// Move the first cube up or down. yield return new WaitForSeconds(0.5f); ChangeSize(0, -1.5f);
// Move the second cube up or down. yield return new WaitForSeconds(1.5f); ChangeSize(1, 0.0f);
// Move the third cube up or down. yield return new WaitForSeconds(0.75f); ChangeSize(2, 1.5f);
// Pause for a second. yield return new WaitForSeconds(1.0f);
exampleInUse = false; }
void OnGUI() { GUI.enabled = !exampleInUse; GUI.skin.button.fontSize = 24 * width / 800;
if (GUI.Button(new Rect(xPos, yPos, xSize, ySize), "Move cubes normally")) { Time.timeScale = 1.0f; StartCoroutine(Example()); }
// Set the speed of the cubes to be more fast than normal. if (GUI.Button(new Rect(xPos, yPos + ySize * 1.1f, xSize, ySize), "Move cubes quickly")) { Time.timeScale = 4.0f; StartCoroutine(Example()); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.