Setting up a simple game manager in Unity

Most games will need a game manager to keep track of the progress in the game. Let’s go over how create it.

First, create the game object that will hold the GameManager script. Create the script and attach to the game object. When you create a script with this name Unity will automatically change the icon to a cog wheel:

Open up the script. To make the class easy to access and make sure that there is only one instance of the game manager, we will use a programming pattern called Singleton. To ensure that we limit access, we will use a public property instead of a public variable:

We check if another game manager has already been added to the scene in the Awake function. If another game manager exists, destroy that game object. If no other game manager exists, initialize the instance to itself. As a game manager needs to exist throughout all scenes, call the function DontDestroyOnLoad:

With a game manager it is recommended that you use public properties instead of variables to have better control over access:

Once the Singleton pattern has been set up there will be very little reason to change anything. To make it tidier you can put the code within a region and then minimise this:

Good luck!

--

--

--

Unity / C# Game developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

1. Why Kotlin for Android?

How to import Kioptrix Level 1 into Virtual Box

I have peeked at the dark side

How to Claim Incentivized Testnet Rewards

Our first time arranging a CTF competition

One of the challenges. The origin of the image is unknown, it circulates on Reddit from time to time.

The lost art of the database

How to Build a Multi-Tenant Application Using Spring Boot and Hibernate

Developers Are Mostly Love Starved. Here’s Why?

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
Niklas Bergstrand

Niklas Bergstrand

Unity / C# Game developer

More from Medium

Work (cont.) — Scientific Management

Why the Haitian Struggle Matters for Anti-Racism Activism

Time Series

CS371P Blog 8: Gavin Garcia