- Home /
Problem is not reproducible or outdated
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...
Does your player game object has a rigidbody component attached to it?
You have a reference stored for CapsuleCollider in your capsule variable?
added a picture with what i have in the inspector. Do you think the problem is with the capsule collider because it has no physic material?
I don't think physics material is an issue.
Just to confirm, you have a reference stored for CapsuleCollider in your capsule variable and it is accessible in your current script?
Answer by WhoRainZone1 · Jul 07, 2014 at 05:40 PM
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
looking closer to the full error: NullReferenceException: Object reference not set to an instance of an object SorcererThirdPersonController.PreventStandingInLowHeadroom () (at Assets/Scripts/SorcererThirdPersonController.cs:152) SorcererThirdPersonController.$$anonymous$$ove (Vector3 move, Boolean crouch, Boolean jump, Vector3 lookPos) (at Assets/Scripts/SorcererThirdPersonController.cs:98) ThirdPersonUserControl.FixedUpdate () (at Assets/Imports/Sample Assets/Characters/Third Person Character/Scripts/ThirdPersonUserControl.cs:80) I think the problem after all is in another script on ThirdPersonUserControl. here is the code from there:
character.$$anonymous$$ove( move, crouch, jump, lookPos );// pass all parameters to the character control script