Script appears as null and not null at the same time


I’m working on a simple 3d shooter that has pillars that disappear if the player tries to hide behind them for too long.

To do this, I detect the presence of the player next to the pillar and if it exceeds a certain amount of seconds, the pilar fades out. Once the player moves away, the pillar returns.

Because my player has many different collisions to detect, I use one script to control all collisions and triggers. Once the collision happens, and time has passed, an event is sent and pillar fades:

    private void OnCollisionStay(Collision collision)
        if (collision.gameObject.tag == "AlertTouch")
            if (collision.gameObject.transform.parent.gameObject.tag == "Pillar")
                GameObject pillar = collision.gameObject.transform.parent.gameObject;
                Pillar pillarScript = pillar.GetComponent<Pillar>();
                alertTouchTime += Time.deltaTime;
                if (alertTouchTime >= pillarScript.disappearTime)
                    alertTouchTime = 0;
                    OnPillarFade.Invoke(0, false, pillar);


This worked great until recently when I started getting error messages:
“MissingReferenceException: The object of type ‘Pillar’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.”

type Pillar is the script attached to the pillar that has the listener for the event:

    private void Update()
        Debug.Log("From update: " + this);

    void HandleFade(float fadeValue, bool colliderEnabled, GameObject pillar)
        Debug.Log("From fade " + this);
        if (pillar == this.gameObject)
            myRenderer.material.DOFade(fadeValue, fadePerSecond);
            myCollider.enabled = colliderEnabled;

I check (pillar == this.gameObject) because there are multiple pillars that all listen to the collision and I need to make sure this is the pillar in question.

From the two separate Debug.log calls I get:

From update: Pillar(Clone) (Pillar)
Pillar:Update() (at Assets/Scripts/Obstacles/Pillar.cs:24)”


From fade: null
Pillar:HandleFade(Single, Boolean, GameObject) (at Assets/Scripts/Obstacles/Pillar.cs:29)
PlayerCollisionDetection:OnCollisionStay(Collision) (at Assets/Scripts/Utilities/PlayerCollisionDetection.cs:40)”

at the same time. How can this even happen? I’m not destroying the pillar at any point, not enable/disable anything.

I would appreciate any help, and thanks in advance

We don’t see how you actually call your “HandleFade” method. My Guess is you just do something like

collision.collider.GetComponent<YourScript>().HandleFade(xxx, yyy, zzz);

Now when GetComponent does not find any component it will return a fake null object when you test inside the editor. In a build an actual null value will be returned. So while you can actually call and enter the method when testing in the editor, you would get a null reference exception in a build.

So make sure you actually look for the component at the right spot and that you actually get a valid component back from GetComponent. For more information on fake null objects, see this blog post.

Note that fake null objects are normal objects from the C# / .NET point of view. However since components (and all UnityEngine.Object derived classes) have a native code counterpart in the C++ engine core having just the managed wrapper class instance is pointless without the actual representation on the C++ side. When you call Destroy on a gameobject you will only destroy the native part. The managed part has to be garbage collected once there’s no reference to it anymore.

All Unity API related properties and methods will check if the object is still alive (has its native representation in the engine) and throw a MissingComponentException if it has not. Since you only call pure managed methods Unity won’t detect this mistake.

Show us your OnCollisionStay method and we can tell you where it might went wrong.