- Home /
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
Yes, i'm calling the coroutine from update. When i debug it the yellow line indicating the line about to be executed sometimes becomes green and using "step into" repeats it a few times. Dunno what this means. In the line where the coroutine is called:
StartCoroutine("$$anonymous$$yCoroutine");
sometimes the yellow line repeats too, and in the first line of coroutine:
IEnumerator $$anonymous$$yCoroutine(){
it happens the same, but then it goes normal, line by line, as usual.
One more thing, when it reaches the yield call:
yield return new WaitForEndOfFrame();
Then it jumps out of while loop (as expected) but before continuing on the Update function it jumps to the last line of the coroutine but it does NOT execute it (i watch the variable on that line and it does not change). After this it jumps back to the Update function and finishes, but ins$$anonymous$$d of begin again the Update function until it reaches the coroutine call, it goes directly to the coroutine call ignoring all the code before that line.
Answer by ThePunisher · Dec 07, 2013 at 12:24 AM
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).
Answer by ParagonSe7en · Mar 01, 2017 at 07:24 AM
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.