NullReferenceException: Object reference not set to an instance of an object. (112717)

in my script i get a NullReferenceException: Object reference not set to an instance of an object
dangerzone.Awake () (at Assets/Levelkit 1/FPSScripts/dangerzone.js:5). the script is a trigger box collider that when it hit the player it will take you 5 point of life .the script works fine but i always see this error and i suspect that it my be the reasons why my game lags

here is my script:

#pragma strict
var healthScript : healthscriptforplayer ;
var animator: Animator;
function Awake () {
 healthScript = GameObject.Find("First Person Controller ").GetComponent( healthscriptforplayer  );
}

function OnTriggerEnter (Col : Collider ) {
if ( Col.tag == "Player"){
    animator.SetBool ("Nattack" , true);
    animator.SetBool("Nwalk" , false);
    healthScript. CurrentHealth -= 5;
}
}
function OnTriggerExit (Col : Collider ) {
if ( Col.tag == "Player"){
    animator.SetBool ("Nattack" , false);
   
}
}

please help.

GameObject.Find("First Person Controller ") Erroneous WhiteSpace in String

euh i'm afraid that it still doesn't work i'm still getting the error. =(

Try changing function Awake () { healthScript = GameObject.Find("First Person Controller ").GetComponent( healthscriptforplayer ); } to function Start() { healthScript = GameObject.Find("First Person Controller").GetComponent( healthscriptforplayer ); }

Are you setting healthScript in the inspector? If so, you shouldn't need to perform a lookup. Anyhow, if GameObject.Find returns null, that means no GameObject with that exact name exists at that exact time.

what do you mean?

1 Answer

1

In the Awake() function some of the game objects in the scene may not exist yet. It is better to look for your “First Pereson Controller” in the Start() function.