As mentioned briefly in my previous article, singletons are a pattern commonly used in programming both in and outside of Unity.

A Singleton is a class with only allows a single instance of itself to be created and accessed. These allow for you to call and store variables from various other sources and reference them from the script utilizing the Singleton pattern.

Singletons has many uses, but the utilization in my case is for my manager class objects (Game Manager, Audio Manager) due to the need to constantly access other scripts to get information. Instead of using GetComponent a dozen times, I can utilize this Singleton to store the information needed until it needs to be called.

Manager class objects are commonly used to manage different functions in gameplay. In this project, I used two; one controls the audio for the game and the other is used to end the introduction cutscene and allow the player control following the end of that cutscene.

These scripts follow a pattern known as a singleton (more on that in another article).

Manager classes can become a dumping pit of sorts, taking in variables and values that did not fit in other places and make them available to reference to a wide variety of other scripts.

This eliminates the need for GetComponent to be used in reference to the object the component is on over and over again!

Jack Leavey

Unity Developer

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