Using Singletons in Unity

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:

--

--

Unity and Flutter developer interested in creating mixed-reality applications.

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
Richard Morgan

Unity and Flutter developer interested in creating mixed-reality applications.