Create a Game Manager
Up until this point we’ve been creating bits and pieces of the game without any coordinating logic between these pieces. We’re going to create a Game Manager script or “class” that will be responsible for running the game logic such as spawning the player if she is killed by her enemies.
Before we start writing the RPG Game Manager script, let’s learn about a software design pattern called the Singleton. Singletons are used in situations where your application needs one and only one instance of a particular class to be created for the lifetime of the application. Singletons can be helpful when you have a single class that provides functionality used by several other classes in your game, such as coordinating game logic in a Game Manager class. Singletons can provide a public unified point of access to this class and its functionality. They also offer lazy instantiation, meaning they are created the first time they are accessed.
Creating the Singleton
Create a new Game Object in the Hierarchy and rename it: RPG Game Manager. Then create a new folder under Scripts called: “Managers”. Create a new C# script called “RPG Game Manager” and move it to the Managers folder. Add the script to the RPG Game Manager object.
We want to be able to create or “spawn” characters—a player, or an enemy, at a specific location in the scene. If we’re spawning enemies, then we may also want to spawn them at a regular interval as well. To accomplish this, we’re going to create a Spawn Point prefab and attach a script to it with the spawning logic. Right-click in the Hierarchy view, create an empty Game Object, and rename it: “Spawn Point”. Add a new C# script to the Spawn Point object we just created called: “Spawn Point”. Move the script to the Mono Behaviors folder.
Build a Spawn Point Prefab
Here’s the plan: we’ll set up a Spawn Point for the player first, just to see how all the pieces fit together, and then we’ll set up a Spawn Point for enemies. To build a generic Spawn Point, add the script we just wrote to the Spawn Point Game Object, and then create a prefab out of it.
Configure the Player Spawn Point
We still have to configure the Spawn Point so it knows what prefab to spawn. Set the “Prefab to Spawn” property in the attached Spawn Point script to the Player Object prefab by dragging the Player Object prefab to the respective property as seen in Figure 7-6. Leave the Repeat Interval set to 0 because we only want to spawn the player once.
A Spawn Point for Enemies
Let’s build a Spawn Point to spawn enemies. Because we’ve already built a Spawn Point prefab, this will be quick.
- Drag and Drop a Spawn Point prefab into the Scene.
- Rename it EnemySpawnPoint. – (Optional) Change the icon to red, so we can view it easily in the Scene view
- Set the “Prefab to Spawn” property to the Enemy prefab.
- Set the Repeat Interval to 10 (seconds), to spawn an Enemy every 10 seconds
To restore the behavior where the camera follows the player as she walks around the map, we’re going to create a Camera Manager class and have the Game Manager use it to ensure the Virtual Camera is properly set up. This Camera Manager will be useful in the future as a centralized place for configuring camera behavior instead of embedding that camera code in various places throughout our app.
The Virtual Keyword
The “virtual” keyword in C# is used to declare that classes, methods, or variables will be implemented in the current class but can also be overridden in an inheriting class if the current implementation is not sufficient. In the following code, we’re building the basic functionality to kill a Character but inheriting classes may require additional functionality on top of this.
Our sample game is really starting to come together. We’ve created an architecture for the various types of characters throughout the game and picked up a few pointers on using C# in the process. Our game now has a central game manager responsible for setting up a Scene, spawning the player, and ensuring the camera is set up properly. We’ve learned how to write code to control the Camera programmatically, where we previously had to set up the Camera via the Unity Editor. We built a Spawn Point to spawn different character types, and learned about Croutons, an important tool in the Unity developer’s toolbox.