indexOutofRangeException for collision.contacts[0].point.x

I have a car that’s hitting the ground. It has 4 wheel colliders and one box collider for the body. I’m spawning an explosion graphic at the point of impact. If the car lands on all four tires I get an indexOutofRangeException when I try and do the instantiation.

function OnCollisionEnter(collision : Collision) {
Instantiate (splode, new Vector3(collision.contacts[0].point.x, -10, collision.contacts[0].point.z), new Quaternion(0,1,0,1));
}

I understand that the out of range means that I’m looking for a value that doesn’t exist, but I’m looking for contacts[0]. Isn’t there always a contacts[0]? Is there a better way to do this?

You could put in a check for collision.contacts.Length > 0 just to be sure.

–Eric

Did you make sure to initialize this array?

Such as;

float[ ] contacts = new float[4];

The contacts array is not something you initialize yourself. Have a look at the docs for OnCollisionEnter and the Collision class.

–Eric