I had a previous post about singletons in Unity3D and have since added a useful functionality to that class. One of the useful features of a singleton is that it is self instantiating. But what if you want to use the Unity editor to expose some public variables and have some other assets hooked into your singleton? So since you are probably using prefabs to manage game components in your scenes anyway, seems like it might be useful to have a self-loading prefab for components such as the player or a gui controller.

Just as singletons allow for really nice static access methods, this would allow me to really easily organize my project as a bunch of prefabs dumped into the Resources directory and when a level needed a particular component it would just use it, no need to instantiate and keep a reference within some big main class, nor keep it in the scene hierarchy at all to begin with.

Here is my updated singleton class. What I ended up doing was adding an Attribute so you could just define the Prefab attribute for any singletons that did have associated prefabs and off you go. I have left in some debug statements commented out for clarity. The only caveat is that of course you could accidentally have added multiple instances of the prefab in your scene -- what I do now is to just throw an exception if that is the case (an alternative may be to just choose one and self-destruct the others but deciding which is correct is unclear).

What do you think? Only other caveats are those associated with using the Resources folder in general such as not being able to strip out components that aren't used. Since my games so far have been small this hasn't been an issue and I know that I should clean out any resources that I'm definitely not using.