Coroutines¶
Give a component time. Wait a second, pulse a scale, fade a color, or chain a few steps, then keep editing the same object in Play Mode.
ZenCoder runs the routine for you. Press Stop, disable the component, or press Run again, and every routine that component started is cancelled. The scene stays clear of leftover waits from the previous version of your code.
1.2.0 · Shoshin
Introduced in the v1.2.0 release.
Start one from any method¶
Start, Update, and the other lifecycle methods stay ordinary methods. From any of them, hand ZenCoder a routine:
public void Start()
{
StartCoroutine(Intro());
}
private IEnumerator Intro()
{
yield return new WaitForSeconds(1f);
transform.position += Vector3.up;
}
Press Run. One second later, the object steps up.
IEnumerator is ready to use. No extra using line is required.
You can also start a routine by its method name, when that method has no parameters:
Let Start itself be the routine¶
If the wait is the whole beginning of the component, change void to IEnumerator:
public IEnumerator Start()
{
yield return new WaitForSeconds(1f);
transform.position += Vector3.up;
}
ZenCoder runs that once when the component starts. The lifecycle menu can insert this for you: open + and choose Initialization → Start Coroutine. That entry is there when the script does not already have a Start method. If Start is already in the script, change void to IEnumerator yourself, or call StartCoroutine from the method you already have.
Update, FixedUpdate, and the other messages stay void. A repeating method would begin the routine again every frame. Start the routine once, from Start or from the frame where the player presses a key.
A routine you can trigger¶
public void Update(float deltaTime)
{
if (Input.GetKeyDown(KeyCode.Space))
StartCoroutine(Pulse());
}
private IEnumerator Pulse()
{
transform.localScale = Vector3.one * 1.2f;
yield return new WaitForSeconds(0.15f);
transform.localScale = Vector3.one;
}
Each press of Space grows the object, holds for a moment, then returns it to its original size.
Stop one, or stop them all¶
Keep the handle when you want to cancel a single routine later:
private Coroutine pulse;
public void Update(float deltaTime)
{
if (Input.GetKeyDown(KeyCode.Space))
pulse = StartCoroutine(Pulse());
if (Input.GetKeyDown(KeyCode.Escape))
StopCoroutine(pulse);
}
StopAllCoroutines() cancels every routine this component started, including an IEnumerator Start().
Stop, turning the component off, or pressing Run again ends them for you as well.
Ways to wait¶
Inside a routine:
yield return nullwaits one frameyield return new WaitForSeconds(1f)waits in game timeyield return new WaitForSecondsRealtime(1f)waits on the clock, even if Time Scale is 0yield return new WaitUntil(() => grounded)waits until something becomes trueyield return new WaitWhile(() => moving)waits while something stays trueyield return new WaitForEndOfFrame()waits until the frame has finished drawingyield return new WaitForFixedUpdate()waits for the next physics step
You can yield return another routine to run it as part of this one:
While you type¶
staoffersStartCoroutinestooffersStopCoroutineandStopAllCoroutinesyieoffersyield return- After
yield return, ZenCoder offers the waits above
A routine that throws is covered by Safe Execution: the first failure pauses the script, and later frames stay quiet.