- Home /
Question by
Max 4 · Jul 24, 2011 at 03:18 AM ·
rigidbodynullreferenceexceptiontag
NullReferenceException about rigidbody tags
I am constantly getting a NullRefenceException on one line
if(collision.rigidbody.tag != "ground")
I don't know what's wrong with it. I have tried changing everything, but it just doesn't work. There is an object tagged "ground".
Comment
Best Answer
Answer by Peter G · Jul 24, 2011 at 03:31 AM
The NRE is probably not coming from the tag
but rather collision reference or more likely rigidbody reference. If your object collides with an object without a rigidbody attached, then the reference will be null. And you can't check the property of a null reference.
Fix: add a conditional to make sure the object has a rigidbody.
if(collision.rigidbody != null) {
//do the rest of the stuff;
}
else {
Debug.Log("The object we hit does not have a rigidbody attached.");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612154025im_/https://answers.unity.com/themes/thub/images/avi.jpg)