Game Programming Pattern: Singleton

Good or Bad? How we can use in Unity3D

Sample singleton that lives for the life of a scene

Previously I discussed how using a GameManager class could help us manage when the player had a key card or not. The GameManager class, much like the AudioManager class above is an example of a singleton.

Singletons or one of the many software design patterns used in software development, and is one of the first we learn as game developers. The singleton allows us to create scripts or classes that will only ever have one instance of itself. This is helpful for managing things like game states, UI interaction or Audio. Along with many other things.

In my example I use it for an Audio Manager that other scripts will call to play sounds in my game. The above portion of code only shows the setup of the instance, and it only lives for the length of the scene (more on that later).

Audio Manger functions

The audio manager has a couple of variables and two public methods. One will change the background music and one will play audio clips like voiceovers, etc. A script only needs to tell the audio manager what it needs to play and the audio manager will play it. There is no need for lots of sound script in other sections of code. This can be very handy when we have lots of things that need to trigger sounds.

To make this live for the life of our game and not just a scene we need to add one more line of code to the Awake() method:

DontDestroyOnLoad(_instance);

This tells Unity that we want this class to live while the game is running. So we can then use the Audio Manager class on every scene we have in our game. Now we can have a GameManager class that lives through the life of the game and we could have individual LevelManagers that only live while a single scene is active.

These managers can all react with one another to help us control our game without a lot of interwoven dependencies between all of our other scripts.

Pros:

  • Single instance of important things like game states, audio control, UI behavior, inventory management, etc.
  • Easily accessed without needing to code a reference to other scripts since the static keyword makes them accessible directly to other scripts.
  • Better alternative than global variables.

Cons:

  • Object/Script load orders can cause issues when the singletons rely on each other.
  • Can not be inherited from.
  • Breaks premise of object oriented programming in several other ways besides inheritance.

The war about the use of singletons will probably rage for as long as we continue to write code. While they may not be inherently bad, nor good I contend they have their place, especially for the aspiring dev who is just dipping their toes into game development.

Turning my passion for video games and 11 years of software development experience into a focus on video game development using Unity3D.

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