Just wondered, if there was any benefit to using Coroutines over Invokes. I would assume that they both use Unity's own internal task management system to trigger events at a certain time, and therefore they should be equivalent, speed-wise. Anyone know for sure? I know, measure and be sure, but it's not a huge issue for me. It's more curiosity that prompted this question, than any real need.
I guess the only difference is that you need one line of code less ( or even an own method less ) if you use Invoke.. -The logic behind Invoke is probabely like a WaitForSeconds + Coroutine method.
InvokeRepeat at least serves a purpose and thats that you can have the same script run over and over again at a fixed time interval
I would assume so, since we get methods for canceling all Invokes and Coroutines. There's an obvious use for InvokeRepeating, but I was curious if Invoke or a method that yielded with WaitForSeconds might be different, implementation-wise.
I find Invoke useful when I have an object with a limited lifetime. For example Code (csharp): void Start() { // do stuff to initialize the object ... Invoke ("SelfDestruct", lifeTime); } As opposed to this Code (csharp): void Update() { elapsedTime += Time.deltaTime; if (elapsedTime >= lifeTime) SelfDestruct(); } Or even this Code (csharp): void Start() { destructTime = Time.time + lifeTime; } void Update() { if (Time.time > destructTime) SelfDestruct(); } Of course if its for something like a projectile (which may meet a pre-lifetime demise), then I just include the following Code (csharp): void OnDestroy() { if (IsInvoking ("SelfDestruct")) CancelInvoke ("SelfDestruct"); } I'm not sure if that CancelInvoke really needs to be done, but I like to clean up after myself. Patrick Edit: And I like resurrecting dead threads... heh Sorry guys. I was just looking for the diff between Invoke and Coroutines and threw in my 2 cents before I realized how old the thread was.
Invokes are dumbed-down coroutines. A coroutine can be called with inputs. If you're not comfortable writing functions, that doesn't seem like much, but once you are, it's a huge advantage. You can also get "handles" to coroutines, used to cancel. For example you could run "glow" twice, on thingA and thingB, getting a handle to each: A=StartCoroutine(glow(thingA,2); B=StartCoroutine(glow(thingB. 6));. Now you can cancel either of them, using A or B to say which one. You don't need this as often, but it's good when you do, and it's also a standard coding trick.
At least MadRobot had the good grace to apologise for his (much smaller) necro But as we're here, if you like using invoke then you might want to check out this asset, I've found it very handy and it gets included in all my projects now https://assetstore.unity.com/packages/tools/super-invoke-53369
I find that Invokes are error-prone (string calling) and limiting (no parameters). After adding the appropriate "ActionInSeconds" coroutines (and several others) to my MonoBehaviour wrapper, calling such an action is just one line of code.
I feel like Invokes are perfect for running functions once asynchronously after a delay. I'm not sure if one can yield within an invoked function, but if one needs to yield often then that's what coroutines are for anyways, whether they're both handled internally the same or not (which I don't know). I actually have a couple of simple functions I implemented with coroutines in a new project which just need to be run after a while and just once, and after checking an old project where I used Invoke for this purpose I'm totally going to switch them to Invokes as I find them more readable and neat.
I'd be curious to see your boilerplate as I've been thinking of doing something similar with a series of coroutines that I seem to use quite often.
Well, a long time ago (around 2014) I created the CoroutineHelper on the wiki. It's probably similar to what he had in mind. You could use the Run class anywhere to easily run delegates independently of the caller. Note that when I wrote this we did not have a StopCoroutine method (well we had, but only the string version).