Component.gameobject is null

I have this property:

        public bool Show
        {
            get
            {
                try
                {
                    if (gameObject != null)
                    {
                        return gameObject.activeSelf;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e)
                {
                    Debug.Log("how does this even happen?" + e.ToString());
                    return false;
                }               
            }
            set
            {
                if (value != Show)
                {
                    gameObject.SetActive(value);
                }
            }
        }

being called from within a class that derives from MonoBehaviour. The try block is throwing a null reference exception. This is on a webGL build.

How is this possible? In what cases would a component not have a gameObject assigned to it?

Okay, it derives from MonoBehaviour. However, is there any instance of this class on the scene? I believe that the component is not equal to object. It is just a class.

This can happen if the behaviour was not instantiated correctly, i.e. created using a constructor rather than AddComponent / drag and drop.

As long as you don’t touch anything from the behaviours properties (for example .gameObject) that wouldn’t go wrong. However, if you do so, Unity’s custom null-check will lead to that exception.