- Home /
NullReferenceException: Object reference not set to an instance of an object.
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.
Answer by smoggach · Aug 06, 2014 at 02:34 PM
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.
Your answer
Follow this Question
Related Questions
NullReferenceException... 1 Answer
troubleshooting NullReferenceException 2 Answers
NullReferenceException 2 Answers
Material[] Object reference not set when instantiating 2 Answers