🤑 Indie game store🙌 Free games😂 Fun games😨 Horror games
👷 Game development🎨 Assets📚 Comics
🎉 Sales🎁 Bundles

Hi All,

Singletons are used often in the game industry, in some projects I've worked on they have been used too often and can end up creating confusion. One or two can be helpful but the more you add the more difficult to follow the code can become. Additionally, you can often achieve the same effect with different approaches.

For example, if you want to force a class to have only one instance, then you can store a static instance on Awake and if you find the instance has already been set then print an error and destroy the extra copy. This method of ensuring a class doesn't exist more than once does not require public access to the instance. If you want a set of helper functions that you can reuse anywhere, this can be a class of public static functions which are accessible from everywhere but the class itself has no state and no instance is required.

When the Gang of Four wrote the original design patterns book they stressed that Singleton should be used sparingly. When it isn't used sparingly it usually indicates that code has been poorly thought out before being written, as there is almost always a more elegant solution.

Also remember that Unity is based on the Object Component pattern, if you do not know what this is then look it up, you should avoid OOP methods when using Object / Component. Write small components that can be reused on multiple objects, rather than inheritance. I'm not suggesting that you can't use inheritance only that there may be better / smarter ways of reusing your code.

If you have any questions then lets discuss them!