Getting an Error (NullReferenceException)

im getting this Error when im entering the Water:

NullReferenceException: A null value was found where an object instance was required.
water.OnTriggerExit (UnityEngine.Collider col) (at Assets/water.js:11)

On the water is this script:

function OnTriggerEnter (col : Collider) 
{
var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);
if (controller != null)
print ("enter");
}

function OnTriggerExit (col : Collider) 
{ 
var controller : ThirdPersonController = col.GetComponent(ThirdPersonController); 
if (controller.IsGrounded != null) 
print("We are grounded"); 
}

Some different collider other than your collider object with the "ThirdPersonController" script is hitting your trigger. function OnTriggerExit (col : Collider) { var controller : ThirdPersonController = col.GetComponent(ThirdPersonController); if ( controller ) { if (controller.IsGrounded != null) print("We are grounded"); } else { Debug.LogWarning( "Something else is hitting this trigger : " + col.gameObject.name ); } } do the same for OnTriggerEnter

* 1 / DON'T POST COMMENTS AS ANSWERS * 2 / Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base. Here at Unity Answers, Answer means Solution, not Response. Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers I have converted this to a comment for you. Also you don't have to wait for a moderator to approve a comment.

1 Answer

1

The object leaving the water does not have a TPC component.

(You do check for null in Enter, but not in Exit…)

alucard's andswer was correct. you need to ensure that controller exists before using it (for every object, it must exist before using it). this is very basic programming stuff, and if you're confused by it then you need to work through more tutorials until it is clear - there's really nothing we can do.