Ok, after years of working with ActionScript 3 I got used to use Greensocks superb tweening library “TweenMax” a lot. In my opinion one of the most useful functions is the “delayedCall” method which lets me execute code with some delay (who would have guessed it).
I was looking for a “one-liner” to do the same thing in Unity3D using C#. And I found it. It’s called “Invoke” and it’s a method of the MonoBehaviour class which all C# scripts inherit from if they are created within Unity3D.
// triggers the method "launchRocket" after 4 seconds
To do repeating calls use “InvokeRepeating” and add an additional parameter to set the delay between each call.
InvokeRepeating("launchRocket", 4, 0.5);
// triggers after 4 seconds and does another calls every 0.5 seconds
To cancel use “CancelInvoke” with the method name as parameter. If you want to cancel all invoke calls on a MonoBehaviour don’t pass any name.
//cancels all invoke calls with the given method name
//cancels all invoke calls on the current MonoBehaviour