StartCoroutine not called in Correct Time

when i was working on my game i noticed that the my not worked properly Because StartCoroutine not called in correct time so i checked it in log .
the deference between logs is not constant.
i called the Coroutine in void Start
so when i apply i find thay the result it deffrenced 0.145, 0.127, 0.122, like that .
how can i solve this problem?
thanks.

IEnumerator DoAfterTime()
    {
        while (true)
        {
            float oldTime = Time.time;
            yield return new WaitForSeconds(0.1f);
            Debug.Log(Time.time - oldTime);
        }
    }

You can’t fix it to be exactly 0.1 seconds as mentioned in the comments. WaitForSeconds waits for a minimum of 0.1 seconds, not 0.1 seconds exactly. This delay can be bigger or smaller depending on your current fps. Low fps or fps drops can mean that the exact time waited is less accurate. Also see the docs.


You can however change it so that instead of waiting for a minimum of 0.1 seconds, you wait for an average of 0.1 seconds (meaning that it is sometimes over 0.1 and sometimes under 0.1 to compensate) That way, even though it is not exactly the 0.1 interval, it won’t stack up over multiple frames and the number of calls over a longer period is still what you’d expect.

IEnumerator DoAfterTime()
{
    float waitFor = 0;
    while (true)
    {
        float oldTime = Time.time;
        waitFor += 0.1f; // Add the time we want to wait
        yield return new WaitForSeconds(waitFor);
        waitFor -= Time.time - oldTime; // substract the time we actually waited
        Debug.Log(Time.time - oldTime);
    }
}