- Home /
Are Monobehaviour Singletons a good practice?
Hi, all, I keep reading all over the place that singletons are bad for a bunch of good reasons that I perfectly understand. But somehow, when I make games, I keep coming back to singletons to handle things like:
Game events
Object pooling
Configuration
Global state
Most of the time, it's due to the fact that, when objects are instantiated from a prefab, there's no efficient way to give those object instances a reference to a specific object on the scene. Except perhaps via a factory, but that feels redundant with the concept of prefab.
The only alternative to singletons I can think of is to do use FindObjectWithTag on Awake, which feels like heresy to me, as it also violates tons of OO principles.
Lots of guides, tutorials and posts seem to be advocating the use of singletons in Unity. So is it because those people are ill-advised, or are Singletons actually OK in unity ?
Answer by Injec · Oct 12, 2017 at 01:52 PM
I always use Singletons for stuff like GameManagers. Variables in a GameManager are often changed by other Scripts, so it's more efficient to get the component using a Singleton (GameManager.Instance.DoStuff()) instead of using GameObject.Find("GameManager").GetComponent().DoStuff(), because string compares are not performant. :)