Join Kelley Hecker for an in-depth discussion in this video Using Coroutine and InvokeRepeating, part of Unity: Scripting with C#.
- While you can use update to do something every frame…in some cases you might only want to run…some code every few seconds, or stop code…from executing when a certain condition is met.…You also might have code that needs to run…over multiple frames rather than…completing everything in a single update.…Two alternatives to using update,…are Coroutines and InvokeRepeating.…InvokeRepeating calls a function repeatedly…at a certain interval.…Imagine you have a cannon that should…fire a shot every five seconds.…If you were using update,…you would have to keep track of a timer…and check the timer each frame…to see if you've reached five seconds.…
With InvokeRepeating, you just have to call…InvokeRepeating with your function name FireCannon…and repeat rate five seconds,…and then the function will automatically…be called every five seconds.…You can also delay the first call to the function,…by using the second parameter.…If you don't want a delay, use a small number like .001…instead of zero.…There's a bug where InvokeRepeating…
- Defining classes and variables
- Attaching scripts to objects
- Understanding MonoBehaviour class
- Using the Unity Scripting API
- Importing assets
- Using a singleton pattern to manage game state
- Creating an event
Skill Level Intermediate
Q: Why am I getting a script compile error when trying to load the exercise files?
A: This course was recorded in Unity 4.6, and Unity has since released Unity 5. There are two items to address for Unity 5 users.
Q: Why can't I see the Render Settings option in the Edit menu?
A: The Render Settings options have been moved in Unity 5. You can access them by going to the Window menu and choosing Lighting. In the window that loads, Render Settings can be found under the Scene tab.