[C# Tutorial] a deeper look into singletons in unity3d

In this tutorial I make a more detailed discussion about the nuances of singleton usage in unity 3d and of the tradeoffs involved.
Also, I explain how to obtain the equivalent of a static class with an inspector.
http://brightreasongames.com/singleton-follow-deeper-look-singletons-unity3d/

Thanks !

I usually do that:

public class LazySingleton : Monobehaviour {
private static LazySingleton instance;
    public static LazySingleton Instance
    {
       get
       {
          if (instance  == null)
          {
              instance = GameObject.FindObjectOfType<LazySingleton>();
          }
          return instance;
       }
    }
}

Since I know that my class is already in the scene.
But many of my projects are small, I see it can be a problem on a long & large dev.
I will think about your Option B: Handcrafting next time !

1 Like

Hi thanks for the reply, even if you don’t use option B I’d suggest to move the FindObject there:

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void OnBeforeSceneLoadRuntimeMethod ()
{
          if (instance  == null)
          {
              instance = GameObject.FindObjectOfType<LazySingleton>();
         }
         return instance;
}

So that the search is not done during the game :wink: