That would be my first intuition.
Generally speaking…
… if you have to use StopCoroutine();
then coroutines are not the correct solution.
If you want to persist with all of this start/stop coroutine stuff it will be harder to debug what’s going on, but at the end of the day you will still need to debug it to find what is happening before you can begin to fix it.
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...);
statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Here’s more random coroutine reading.
Coroutines in a nutshell:
Coroutines are an IEnumerator object. They have their own internal state. It’s different from just calling InvokeRepeating(), which would restart a fresh function call each time.
Here is ALL you need to know about coroutines in Unity:
they are an IEnumerator, also known in some languages as a Generator. It is an object with a .MoveNext() method on it that “yields” the next thing with each subsequent call. That is what you create by calling an IEnumerator: you create the object, you do NOTHIN…
Hey Epoch, here’s how it goes down. A coroutine is just an IEnumerator object, and you make one by just calling a coroutine.
When you do that, nothing happens. It’s like a water pump that needs to have its crank pumped, but nobody is pumping it so it sits there and does nothing.
In Unity there’s a special agreement: if you hand that IEnumerator object into StartCoroutine(), which is a method belonging to the object where that StartCoroutine() call is located, what Unity does is starts “pumpin…
Splitting up larger tasks in coroutines:
Technically, yeah, there’s always a way. But in practice, in the general sense it can get really hairy.
Also, in the particular case of adding lots of GameObjects, I know they’re not linear: last I checked, adding 1000 GameObjects is WAY more than 10x slower than adding 100 Gameobjects, I presume due to inefficiencies in newly-added stuff, not sure/ No way to tell with a closed-source engine like Unity.
Trying to tie into system timers and judge how long things have taken so far will get you …
Coroutines are NOT always an appropriate solution: know when to use them!
“Why not simply stop having so many coroutines ffs.” - orionsyndrome on Unity3D forums
The main thing to check before calling / making a coroutine is to ask yourself,
“Does this even need to be a coroutine?”
Generally, if your design requires you to call StopCoroutine(), then don’t make it a coroutine.
If you just need something to happen a bit later, this is a more-robust approach:
https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e
If you’re flipping from one value to another, such as fading in, fading out, changing speeds slowly, etc., coroutines are an AW…
Again I would not reach for coroutines here.
The reason: you can go below the critical health, then go up, then go back down, and every single point in there is a chance to screw up how many coroutines are actually running.
Coroutines are great but they are the wrong solution for this problem.
Instead, make a heartbeat script and have a variable to count if it is time yet:
private float heartBeatYet;
In your Update() function, calculate the desired rate of heartbeat based on health:
float …
Our very own Bunny83 has also provided a Coroutine Crash Course:
I’m kinda suprised that until now nobody has asked a question like this. Even though I have explained what a coroutine actually is several times in other questions it just shows that there is still quite a misunderstanding what a coroutine actually is.
Lets just step back for a moment. Unity implements coroutines by utilizing a C# feature called “iterator blocks” or iterator methods (which are more generally known as Generator methods ). C# ships with two relevant interfaces (IEnu…
And another overview by Anders Malmgren:
You need to understand the basics of what the Unity coroutine system is based on. Its not magic, its based on the fundamental basics of C# 3.0 which introduced the iterator pattern as part of their Linq feature.
Any method that returns IEnumerator or IEnumerator<T> or IEnumerable/<T> can be iterated upon.
This is actually a very easy concept. The IEnumerator interface as follows
public interface IEnumerator
{
bool MoveNext();
object Current { get; }
void Reset();
}
Its…
3 Likes