Join Kelley Hecker for an in-depth discussion in this video Using Update instead of Coroutine, part of Unity: Scripting with C#.
- Like I mentioned in an earlier video,…you could create the spinning, floating coin…using Update instead of Coroutines.…I'll now show you how to modify the coin script…to use Update.…Remember to input the additional assets…from the Asset Store if you haven't already…as I showed you earlier in this chapter.…Open the coin script.…The first thing we can do…is remove the StartCoroutine calls…from the start function…and change the return type of the Spin and Float functions…to be void instead of IEnumerator.…
We also need to remove the yield functions…because those can only be called…within a function that returns IEnumerator.…Now Spin and Float are just ordinary functions.…Most of the code inside Spin and Float can stay the same.…Our code was designed to run every frame,…gradually moving the coin over time,…which is what we want for a function…that's going to be called from Update…but we do need to remove the while loop.…
Before, we were using the while loop…to make our code execute every frame…but now, Update will do that for us.…
- 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.