Weird coroutine behaviour, speed increases too much

Hi guys.

I have 3 objects with the same script attached. This script has a coroutine. For any reason Time.deltaTime is not working the same way in Update function and inside the coroutine. The visible result of this is Time.deltaTime grows very significantly inside the coroutine, and as i’m using Time.deltaTime to move this objects along a path at constant speed, when the first object reaches a certain point in the path where the coroutine takes control the speed of the object running the coroutine is x20 normal speed (the other objects keep moving at normal speed until they reach the same point). The coroutine calculates the speed the same way than Update function:

Position = Position + Time.deltaTime/TotalTimeOfPath;

When i debug the game i see the values of Position grow faster (and constantly) inside the coroutine. Do you know what is happening?

Possibilities: Or you are updating Position multiple times or you are calling the coroutine to often. Debug it and look how often the coroutine is called in a single frame. Also, are you calling it from Update()? Time.deltaTime is working for Update() not for FixedUpdate()... If you are using events, then check them to. Can't say much more without a somehow more complete code of your's. Greetings Chillersanim

2 Answers

2

Since you mentioned you are calling your coroutine from update I’m going to assume that it is not checking if the coroutine has already been started so the update loop will start yet another coroutine on future updates. Eventually you get many coroutines updating the object’s position and it gives you that effect you speak of (where the speed increases dramatically). Wrap your StartCoroutine call with an If condition that makes sure it does not get called while the same coroutine is already running.

You can do this using a flag like such:

private bool m_coroutineStartedFlag = false;

    private void Update()
    {
        if (!m_coroutineStartedFlag)
        {
            StartCoroutine(MyCoroutine());
        }
    }

    private IEnumerator MyCoroutine()
    {
        m_coroutineStartedFlag = true;

        //do code and yielding until done.

        m_coroutineStartedFlag = false;
    }

Wow, i haven't test it yet but i'm pretty sure you're right. Gonna try it and feedback to you

Yes, you were right. I'm calling the coroutine once per frame. I think the flag solution is the only in this scenario, i'm actually thinking that maybe this is not the best use for a coroutine. As i have written all the code in update function now i must decide if delete update function and use only coroutines or use the flag solution massively (i began using coroutines to avoid flags). Thank you very much.

Why in the world would you want to use a coroutine every frame, isn't that's what Update is for? If it's really a 2 step process you could use Update and LateUpdate. Or you could just call a new function from Update which is not a Coroutine. But yes, Coroutines if implemented incorrectly can multiply out of control, I've seen quite a few questions here in the past involving such problems.

The reason, Ryan, is this: http://answers.unity3d.com/questions/588015/how-to-change-this-using-a-coroutine.html but now i'm changing my code, i will delete Update function at all and use only coroutines and OnGUI (i will have to use nested coroutines, i hope it will not happen the same).

This thread is super old but I found that I had a similar issue that was being caused by a debug statement that I had in a while loop that was in a coroutine I was running. Hopefully this helps some poor lost soul.