# Timing of coroutines in Unity is never precise

I was looking an official example for coroutines in Unity here.

However, when I call WaitAndPrint the time taken is never exactly 5 seconds.

IEnumerator WaitAndPrint()
{
float timeStart = Time.time;
yield return new WaitForSeconds(5);
Debug.Log(string.Format("Time taken = {0}", Time.time - timeStart));
}

if (Input.GetKeyDown(KeyCode.R))
StartCoroutine(WaitAndPrint());


This is most likely down to the frame rate, which makes me wonder how can I make sure that a coroutine takes the precise amount of time it should take despite the fluctuating frame rate?

• I'm wondering what Veselin asked: why does this matter? – jhocking Jul 21 '14 at 16:06
• Precise timing of musical events at high tempi? – user1423893 Jul 23 '14 at 12:37
• That's a pretty obscure use-case. Unity isn't designed for that kind of thing out of the box; you probably need to write a plugin using native code. – jhocking Jul 23 '14 at 18:04

This is most likely down to the frame rate, which makes me wonder how can I make sure that a coroutine takes the precise amount of time it should take despite the fluctuating frame rate?

I don't think you can, honestly. Coroutines are at the mercy of Update() in the same way the rest of your code is. You can't make it end between two update loops, because that's the time Unity is spending doing other stuff instead

I am kind of wondering why you need that sort of precision to begin with, though. Does it really matter if the coroutine ends when the frame starts, or 3ms before that?

• This sort of precision would be required when generating musical events at regular intervals at higher tempi. Subdividing of pulse and the like. – user1423893 Jul 23 '14 at 12:42

Coroutines aren't threads or processes or anything special like that. They're just callbacks given to the engine to call when a specific condition is met. If you yield null, the coroutine will be called every frame.

Every frame, the engine calls all coroutines that are ready at that time, all together in a block (either right before or right after calling all the .Update()'s, not sure which). It will only call a WaitForSeconds routine AFTER that number of seconds has elapsed, so it will come up during the frame after that time has passed.

You can only really do things in unity in increments of frames (or physics updates), anyway. You surely can't draw something to the screen at a specific exact time, you have to wait for the next frame to happen.

Here's an in-depth article on how coroutines work, and a couple threads that I've bumped into in unity-land relating to them, which link off into other resources as well:

http://www.altdev.co/2011/07/07/unity3d-coroutines-in-detail/