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

💫DISCIPLINA features💫

Time Series Data Analysis Using MongoDB

How to send photos to Telegram in Ruby

Native Run-Time Performance for a High-Level, Dynamically Typed Programming Language

Avoid writing expensive code in Python with Memoization

My Go To For Website Hosting

Windows Kernel Exploitation: THE SAGA | HEVD Writeup | Part -2 Payloads and Processes

PostgreSQL TRIGGERS and \copy working in tandem.

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

Fashion Meets Computer Vision

My Wish for AdTechies Flocking to Web3

Use Case: Enable a Gifting Experience for Crypto & NFTs w/ the Vouchr SDK

What does Ray-ban’s Entry into NFTs Mean for Brands in 2022? | OpenGrowth