Unity Singletons bug

Hi, I just finish to learn singletons and I want to report a Unity bug that cause crash (not all the time).

I created 3 scripts: MonoSingleton.cs, GameManager.cs and Player.cs (project package attached to this thread).

What I did in the property of MonoSingleton.cs is instead of checking if the private field was null like this:

public static T Instance
        {
            get
            {
                if (_instance == null)
                    Debug.Log(typeof(T).ToString() + " is NULL.");

                return _instance;
            }
        }

I checked the property itself causing a stackoverflow exception :stuck_out_tongue:

public static T Instance
        {
            get
            {
                if (Instance == null)
                    Debug.Log(typeof(T).ToString() + " is NULL.");

                return _instance;
            }
        }

When I pressed play the first time, Unity crashed.

5265753–526929–Singletons.unitypackage (3.42 KB)

if you havnt, make sure you file bugs with unity. they dont see every forum post. you can find bug filing info here. :slight_smile:

Sure! I will do it asap.

It’s not a Unity bug. A property inside the same property must not be used because an infinite cycle occurs (to get the property, you have to get the same property but to get the same property you have to get the property but to get the property … :))

It’s not the infinite cycle the bug, but the crash of Unity if you run it. Anyway, I’m pretty sure they fixed it in later versions with an error message at the console. :slight_smile: