- Home /
Should I use Singleton for every component class (e.g Spawner, Mover etc), Or does unity guarantee to have only one instance of those classes, if they are attached to one game object 'Grid'?
I'm making game like Tetris. So I have grid consisting of tiles. I have multiple components attached to my grid gameobject, like Spawner, Mover etc. Now this grid gameobject should be only one, in my game scene, I don't see what could lead to having multiple grids. So logically I think that I will have only one instance of spawner as well. But many sources suggest to have singleton for classes that should have only one instance. Now I'm sure that I only need one instance of Spawner and Mover, but do I need to make them singletons? Or is it guaranteed to have only one instance if they're attached to grid game object? Honestly it looks ugly to turn all this classes to singletons, and than use object.Instance. I'm not quite sure why this is needed, maybe Unity does some background job and it could lead some gameobject components to have multiple instances, but that would ruin everything, so I'm thinking that it should be fine to leave it as it is.
Answer by Legend_Bacon · Dec 04, 2018 at 09:22 AM
Hello there,
Just because a class is only present once in your scene, doesn't mean that it necessarily needs to be a Singleton.
If you only ever put one instance of a script in your scene, then yes it is guaranteed that it will always be unique.
You may want to watch out for what happens when you load new scenes though: if you mark an object as DontDestroyOnLoad(), it will be transferred over to the new scene, which may cause you to have two instances of your class.
As for the Singleton pattern itself, it's kind of a controversial topic. I use it and am happy with it, but I've seen horrible implementations of it too. There are whole debates (like this one) about it. Feel free to take a look.
I hope that helps!
Cheers,
~LegendBacon
Your answer
![](https://koobas.hobune.stream/wayback/20220612183311im_/https://answers.unity.com/themes/thub/images/avi.jpg)