
We all know about the benefits of using Singletons in our games - you just call YourClass.Instance, and wala! A new GameObject gets created with the class attached immediately at run-time if its not already there. Magic! The common Singleton class for Unity3d can be found at the Unity3d wiki page.
While the Singleton design pattern for scripting has been a revelation, what about the same concept applied prefabs with exposed values or predefined configurations? They will be in the likes of Game Manager, Player Controller, SFX/BGM manager, Message Boxes, and many more.
While the Singleton design pattern for scripting has been a revelation, what about the same concept applied prefabs with exposed values or predefined configurations? They will be in the likes of Game Manager, Player Controller, SFX/BGM manager, Message Boxes, and many more.
For my own projects, I have implemented a modified version of the Singleton class from the Unity wiki page, and it's been super convenient for me since. So I'm gonna share the code with you and I hope it helps you too! Do leave some feedback if you found it helpful, or thought of a better implementation, or if you've faced other challenges using this method.
I'm pasting the code below and also uploaded it to Github, so feel free to check it out!
I'm pasting the code below and also uploaded it to Github, so feel free to check it out!
SingletonPrefab.cs