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”.

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.");
}