I've this script in UNITY to check if the hit RayCastHid2D variable has some gameobject with hole tag or a parent gameobject with hole tag when I Raycast...

if (hit.collider != null && (hit.collider.gameObject.tag == "hole" || (hit.collider.gameObject.transform.parent.gameObject != null && hit.collider.gameObject.transform.parent.tag == "hole")))

Everything is fine until if (hit.collider != null && (hit.collider.gameObject.tag == "hole" ||

but when I enter (hit.collider.gameObject.transform.parent.gameObject != null && hit.collider.gameObject.transform.parent.tag == "hole") it throws a NullReferenceException....

But why?

It supposed to short circuit here hit.collider.gameObject.transform.parent.gameObject != null && if there's no parent gameobject!


1 Answer 1


I found an answer :D

It'll be hit.collider.gameObject.transform.parent != null


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .