Easy Singleton in Unity

Daniel Kirwan
2 min readSep 4, 2024

--

If you’ve never used the singleton pattern in Unity for game development it can be very useful. I use them for Managers in a scene that will only be needed for that scene. Which led me down the road of creating a generic class for singleton use. As I got fed up with re-implementing the same code for creating a singleton.

You can download my generic singleton class from GitHub: https://github.com/danielkirwan/MonoSingleton

Just download and add the class to your project and you’re pretty much set up to use it.

I’m showing my GameManager class as an example for implementation. All you need to do is instead of inheriting from MonoBehaviour you replace that with MonoSingleton and add the class you would like as a singleton into <>.

Next, you just need to handle whether or not you would like your singleton class to destroy or not when going between scenes. I’ve added a bool that you can use either in code or in the editor.

If you want to destroy the singleton when going between scenes, just tick the box in the editor or in start in your class.

The bool in question is:

 bool _wantToDestroyOnLoad;

That’s it for this article. Come back soon for more.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet