Sample Assets Third person Controller Error

I am using the Third Person Controller script from the Sample Assets on a instantiated player object and I keep getting these errors that I dont realy know how to solve.
Please Help me!

void PreventStandingInLowHeadroom ()
{
	// prevent standing up in crouch-only zones
    if (!crouchInput) {
		Ray crouchRay = new Ray (rigidbody.position + Vector3.up * capsule.radius * half, Vector3.up);
		float crouchRayLength = originalHeight - capsule.radius * half;
		if (Physics.SphereCast (crouchRay, capsule.radius * half, crouchRayLength)) {
			crouchInput = true;
		}
	}
}	

The error is on the Ray crouchRay row and it says :NullReferenceException: Object reference not set to an instance of an object.


I am not quite sure what reference I am suposed to set…

Heho,

you need to add an “out hit” in C#.

        void PreventStandingInLowHeadroom ()
        {
        // prevent standing up in crouch-only zones
        if (!crouchInput) {
        Ray crouchRay = new Ray (rigidbody.position + Vector3.up * capsule.radius * half, Vector3.up);
RaycastHit hit;
        float crouchRayLength = originalHeight - capsule.radius * half;
        if (Physics.SphereCast (crouchRay, capsule.radius * half, out hit, crouchRayLength)) {
        crouchInput = true;
        }
        }
        } 

Not tested, but that should be the issue

edit:

If you then want to access the raycast hit, use hit.transform.position for positions and hit.collider.name == gameObject.name to detect if you clicked a specific object.

Cheers, wrz