I've just begun doing some basic work with Coroutines and have a quick question. I know you can stop a Coroutine and resume it if it's an infinite loop, but what if at some point it does finish executing? Can you revive the Coroutine and have it run again? Or is it better to create a function with stored code that would create a new Coroutine with the stored code each time you want to run the operation again? Any insight would be appreciated, thanks!
Quick Question about Coroutines?
1 answer
Coroutines
Coroutines aren't actually designed to do things 'at the same time' as another. They still get stuck on the scheduler just like everything else, and you'd get the same (Albeit better) effect via spawn.
Coroutines do however find use in generators, and other stateful systems which will benefit from being able to return multiple times or to be stopped and then resumed at a later time (But only if you're explicitly resuming it from your code; else use spawn or delay as recommended).
If a coroutine finishes, then it is dead and can not be resumed. You can check this with coroutine.status, and spawn a new version of the function if necessary, however if your code runs into a case where you need to respawn a dead coroutine, you may need to reconsider how you're using the coroutine as you'd be better off simply using a normal function.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
coroutines aren't really for running code 'in the background' (that just happens to be one application of them). If you're just using them to fire off code, you might find
spawnanddelayto be simpler