- Home /
Coroutine Errors
So I'm writing this because I've recently crashed into this tree and hit every branch on the way down, and there is very little information about it here, this is for future reference.
First problem: Why is my coroutine running on old information? Ex:
startCo("x"); startCo("y");
x changes a variable and y was supposed to use that variable to do something
How a coroutine works is when its called it accesses all the information it needs to operate and saves at the START of its call. So if you start 2 coroutines in the same function /essentially the same time then all their relevant variables become locked within them before you get the chance to change the variables they use. so even if you spread it out over 30mins it still is using the same variables at the start of its call.
Second, How come this function is being called multiple times?
I'm not sure on the exact technical reason behind this but basically if you have a coroutine and accesses a lot of global variables on itself and externally, changes a lot of things externally (calling multiple function, changing variables), and especially instantiates something the chance for this bug increases and it is compounded by how long the couroutine is up for as well as the severity of any present lag during its operation. The biggest culprit is Instantiating an object and then say calling a constructor on the object. To overcome this bug you basically have to address these circumstances. even if there is no yield between an instantiation and constructor try moving that code to a new non coroutined function.
If there are any more hardcore programmers who can better explain this I have enabled this for community editing.
Your answer
Follow this Question
Related Questions
I can't start a coroutine. I get a weird message from visual studio 1 Answer
Had difficulties implementing intro to Coroutines from unitypatterns.com. Help? 1 Answer
Coroutine sequence not running properly 1 Answer
Coroutine not working - what I did wrong? 2 Answers
Why does this co-routine not function properly after re-entering the scene? 0 Answers