Join Kelley Hecker for an in-depth discussion in this video Using a singleton to manage game state, part of Unity: Scripting with C#.
- We're now going to use our Singleton base class to create…a game manager that will keep track of the game state.…If you're starting from the exercise files…instead of continuing from the previous video…remember that you'll need to import the standard assets…for Unity 4.6 package from the asset store…and drag in the third person character…and free look camera rig prefabs…like we did in the previous chapter.…In the scripts folder, create a new…C# script and call it game manager.…
Double click to open it.…Rather than extending from MonoBehaviour…this class will extend from the…Singleton base class we just created.…Since it's a generic class remember…to replace T with this class, GameManager.…First, we're going to add a property…that keeps track of the time remaining in the game.…We also need a variable that is…the maximum length of the game.…I'm going to copy those from the exercise files.…
I want a game to last five minutes but since…the time is in seconds I have to multiply it by 60.…When the game starts I want to set the timer…
- 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.