Nerd For Tech
Published in

Nerd For Tech

Creating manager classes in Unity

Yeah, if you can get to spawning that alien ship, that would be great!

Generally when we first start programming in Unity, we tend to just script things where we need them without generally organizing them. And that’s perfectly valid! That’s what Object Oriented programming is for, but sometimes you want to be a little more organized and create manager class that helps…well…manage things! More specifically, managers help control the state and flow of your game. You have your GameManager which even has its own special icon in unity

GameManager classes are usually paired with singleton patterns which help make them easily available everywhere, and I’ll go into more detail about that tomorrow, but for now just know that if you want to say, modify the amount of ammo you have for a spaceship, you don’t have to get the player via a GetComponent<> and then access the ammo variable through that, you can essentially just call up the GameManager!

In my case for the great fleece, the GameManager is keeping track of things like cutscenes, and keeping track of important in-game elements like the security card key. when the player grabs it, the GrabKeyActivation script tells the game manager the player now has the key.

When the player reaches the vault door, they touch the WinStateActivation trigger box, which checks to see if you have the card:

The GameManager makes for a perfect middleman taking care of jobs to allow the rest of the game objects to do their jobs smoothly.

UI Managers are also a perfect use of manager classes. I remember many instances of starting game programming where I would access the UI from various places like the player, the enemy, and even the main game, one could easily lose track of things if something went wrong. GalaxyShooter taught the concept of a UIManager where there was one centralized script that everyone would announce to update the UI. There were some extra steps, but it made my code a lot more organized.

And with UI, there are also Audio Managers, Score Managers, etc. The point here is you want to create a system that works elegantly together and is easy to follow and track.

Making a game manager class is easy. Just create a script called GameManager.cs, And here is the heart of the code for your game manager:

If it looks like a bunch of unknown commands, don’t worry, I’ll lay it all down tomorrow, but in a nutshell, this is a singleton pattern that will help make the GameManager available to any outside class by making it a static instance. Now if you have any public variable or function, you can access it with GameManager.Instance.AnyPublicThing!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Esteban Ibarra

Esteban Ibarra

cartoonist, game artist, and wanabe gamedev.