# Null reference on gameObject that has been successfully referenced [duplicate]

It's exactly as the title says. I am getting a null reference error even though I have properly referenced the object. I have debugged my code to show that the result is always exactly what I need however whenever put into practical use, the code returns an error.

                if(hit == true && canSee.transform.CompareTag("Unit"))
{
//var Obehaviour = other.gameObject.transform.parent.gameObject.GetComponent<UnitBehaviour>().Health; <--- Error is here.
float Health = other.gameObject.transform.parent.gameObject.GetComponent<UnitBehaviour>().Health;
float fullHealth = other.gameObject.transform.parent.gameObject.GetComponent<UnitBehaviour>().MaxHealth;
//Debug.Log("Object " + other.gameObject.transform.parent.gameObject);
//Debug.Log("Component and name " + Obehaviour.Name);
ReferenceScript.GetComponent<UnitBehaviour>(other.gameObject.transform.parent.gameObject);
if(Health < fullHealth)
{
if(fullHealth - Health < Ubehaviour.supplies)
{
Health += Ubehaviour.supplyTransferRate;
Health = Mathf.Clamp(Health, 0, fullHealth);
other.gameObject.transform.parent.gameObject.GetComponent<UnitBehaviour>().UpdateHealth();
Ubehaviour.supplies -= Ubehaviour.supplyTransferRate;
Ubehaviour.supplies = Mathf.Clamp(Ubehaviour.Health, 0, Ubehaviour.maxSupplies);
Ubehaviour.LoseSupplies();
}
else
{
return;
}

}
}


As shown in the code above, I have tried to get the values I need by storing them in variables other than caching the entire class. But this also didn't work. What could be wrong? The debugs below work perfectly as well.

• How can the error be on a comment line? Jan 19 '20 at 2:54
• "Even though I have properly referenced the object" is not a claim we can verify from the information present here. Please edit your question to include a Minimal Complete Verifiable Example so that we can reproduce the problem you're seeing and test solutions. Jan 19 '20 at 3:24

var Obehaviour = other.gameObject.transform.parent.gameObject.GetComponent<UnitBehaviour>().Health; <--- Error is here.


There are 3 reasons why this line could throw a NRE:

• other is null
• other doesn't have a parent
• the parent gameObject doesn't have a UnitBehaviour

Keep in mind that when you put this code snippet into some event handler (I suspect you have it in some OnTrigger* or OnCollision* method) then it might get triggered by an object you weren't expecting. It's always dangerous to have long chains of dereferentiations without any checks in between if one of them returns a null value.

If I was to debug this problem in my own code, I would break it up into individual lines so I can see which dereferentiation actually fails:

GameObject otherGameObject = other.gameObject;
Transform otherParent = otherGameObject.transform.parent;
UnitBehaviour parentUnitBehaviour = otherParent.gameObject.GetComponent<UnitBehaviour>();
var Obehaviour = parentUnitBehaviour.Health;


Another thing you can do is to wrap the whole code section into a try { ... } catch NullReferenceException e { ...} block and set a debugger breakpoint in the catch block. When the breakpoint triggers, you can examine what exactly you got in other. You might be in for a surprise.

• Yeah I found the problem, the trigger was taking 2 others, since I had 2 colliders for a certain reason. One was a child of the prefab. One was the prefab. So it returned an error when it found the prefab instead of the child. Thanks!
– Kiyo
Jan 20 '20 at 21:42