How can throw "Missing reference exception" this gameObject?.Getcomponent<>() ??

Hi!

I am just curious.
My line of code

source?.GetComponent<Energy>()?.AddEnergyPercent(gainPercent * 0.01f);

throws “Missing Reference Exception”. It is easy to fix by adding

if(source != null)

before it. The “source” is a GameObject, and it can be sometimes null, and it’s not always have “Energy” component. But the “?.” operator is not for check it is null or not?

All the native implicit null checking operators ?., ??, … work on the base System.Object. However objects derived from UnityEngine.Object have a special == and != operator overload which does detect fake null objects as being null.

This is actually mentioned in the documentation:

This class doesn’t support the
null-conditional operator (?.) and the
null-coalescing operator (??).

Fake-null objects are an important requirement due to the fact that Unity is a C++ engine with explicit memory management and C# / .NET is a manged environment. Objects derived from UnityEngine.Object have an actual partner on the native code side. You can not destroy objects explicitly in the managed world. However we have the Destroy method to destroy any objects derived from UnityEngine.Object. What the method does is actually destroying the object on the native side and mark the managed wrapper object as “being destroyed”. Such an object will essentially pretend that it is null since it’s no longer useable since the native counterpart is missing. That fake null object will eventually be garbage collected once all references to it are gone.

GetComponent plays a special role here but only when testing in the Unity editor. If GetComponent can’t find the component you’re looking for it actually returns a fake null object inside the editor. That way Unity can display a more proper error message (the “Missing Reference Exception” you saw) instead of getting an ordinary null-reference-exception. When running the same code in a build this should not happen.

Though you should be aware of that fake-null state of objects as it’s relevant in other cases as well. Anytime you keep a reference to an object that somehow got destroyed it becomes a fake-null object. As you can read in the blog post it might have been better to not use an overloaded == operator but a seperate “IsAlive()” function. Though that wouldn’t really help with the ?. or ?? operator as they only check for pure null values.

A common example is this:

// this also creates a fake null object because it has no native counterpart
MyMonoBehaviour obj = new MyMonoBehaviour();

// this will be evaluated to true, since it is fake null. Since the variable type is
// UnityEngine.Object derived the custom == operator will be used.
if (obj == null)

// This will NOT evaluate to true since operators are not virtual methods.
// In this case the System,Object == operator will be used.
object o = obj;
if (o == null)

So while the null-conditional and null-coalescing operators do work in some cases, it’s generally dangerous to use them for objects derived from UnityEngine.Object. Not being null does not mean it can be used.

Hello!
I don’t really understand your question, but regarding the null-condition operator, here is some explanation that might clarify it for you:

a?.b

is the same as

if (a == null)
{
    a.b = null;
}

So if a is null, then a?.b is null.

So what is probably happening is that in your case when source is null, then source.GetComponent() will be null, and then your line of code becomes:

null.AddEnergyPercent(gainPercent * 0.01f);

which is probably why you get that error.