I understand the idea is to stop needing to implement the singleton pattern on every class we want to be a singleton, but is there any other reason to implement this code? Can I just continue to implement singleton everytime or would I be missing something by doing that?
You can roll your own Singletons if you wish. Stephen is just teaching a sort of “cool trick” with Singletons, that is all.
Yup, I have been using this type of singleton for years. It certainly beats having to type all the same code over and over. Once it exists, it’s simply a matter of inheriting from the Singleton and, if you need the Awake
, override it and call base. I’ve extended it to Singleton
and SingletonPersistent
(with the DontDestroyOnLoad
only existing in the SingletonPersistent
) so that I could have singletons that are only in the scene I’m currently in. I don’t often use this ‘non-persistent’ version, but it’s there
I try to use as few Singletons as possible in my own projects, but when I do, it’s a Singleton<T>
. One important bit that I don’t remember if Stephen covered in the Singleton class… clearing the Instance value in OnDestroy(), and if you’re using options like not reloading the domain, also clearing it in OnApplication Quit.