Caching Coroutine vs simple bool

Is there an advantage to caching the coroutine over using a simple bool?

In my solution I set the bool to true at the start and false at the end. Then I check the status of that bool before starting the coroutine.

Caching the coroutine seems to do the exact same thing but setting a value to null instead of false.

Is there an advantage to this approach?

I know that the bool doesn’t account for the coroutine being stopped by something else before the bool is set to false, but as I understand it, neither does caching the coroutine. The cached coroutine is only null if it has never been run or it was manually set to null by reaching the end of the coroutine.

I don’t know much about how c# works under the hood but it seems to be a simple bool would be a smaller use of resources than a reference to a coroutine

I think it was a teaching point on showing how to cache a co-routine.
The advantage of caching a coroutine is that we can reuse it when a bool is just checking if its completed in a sense.

Hope this helps

How do you reuse a coroutine from a cached reference and is there a reason to do that over just starting the coroutine normally?

Just trying to make sure I’m not missing something because to me the bool works just as well and I prefer writing If(isRunning) rather than if(cachedCoroutine !=null).
To me, it seems like the only reason to cache a coroutine is if you needed to stop it, rather than just not start a new one.

Privacy & Terms