Using Singletons in Unity

Richard Morgan
Jan 10, 2023

A singleton is a special class that can only have one instance. Because of this, it’s super-easy to reference functions inside a singleton from another class. Singletons can talk to other singletons, and non-singleton classes can talk to singletons, but singletons can not talk to non-singleton classes.

Here’s an example were we use singletons to send debug messages, change a text box, and instantiate a prefab:

Without using singletons, we might try and access a ui component from a player script like this:

But using singletons, the method calls to the singletons are made from the player script like this:

And here are our GameManager, UIManager and SpawnManager scripts: