0

As the title says. I'm getting a NullReferenceException for a split second, and after that, everything Works just fine. Could anyone tell me why this happens?

Here's my code, error's from line 43:

    #pragma strict
//NOTE: Display the health of the enemy that is targetted by the player!

//private var enemyGO : GameObject;
//private var enemyScript : EnemyAI;

var healthText : GUIText;


var myPlayer : Transform;
var myPlayerScript : PlayerScript;

var myPlayerTarget : Transform;
var myPlayerTargetScript : EnemyAI;


function Start () {
    myPlayer = GameObject.FindGameObjectWithTag("Player").transform;
    myPlayerScript = myPlayer.GetComponent("PlayerScript");

/*  if (myPlayerScript.target != null) {
        myPlayerTarget = myPlayerScript.target;

        myPlayerTarget.GetComponent("EnemyAI");
    }*/

//  enemyGO = GameObject.Find("Enemy");
//  enemyScript = enemyGO.GetComponent("EnemyAI");
}


function FixedUpdate () {
    if (myPlayerScript.target != null) {
        myPlayerTarget = myPlayerScript.target;

        myPlayerTargetScript = myPlayerTarget.GetComponent("EnemyAI");
    }
}


function OnGUI () {
    if (myPlayerScript.target != null) {
        GUI.Label (Rect (((Screen.width / 2) + Screen.width / 3), 16, 250, 20), "EnemyHP: " + (Mathf.Round(myPlayerTargetScript.curHealth)) + " / " + /*(Mathf.Round(*/myPlayerTargetScript.maxHealth/*))*/);
    }
}

Thank you :)

EDIT: This doesn't show lines :P So the error is happening in the GUI.Label... line in my OnGUI function :)

  • What is the full error? You have several variables in OnGUI that could be `null` depending on implementation: `myPlayerTargetScript`, `myPlayerTargetScript.curHealth`, `myPlayerTargetScript.maxHealth` – Jerdak Aug 29 '13 at 18:11
  • Ahh right, sorry i forgot that, here it is: NullReferenceException: Object reference not set to an instance of an object EnemyHealthScript.OnGUI () (at Assets/Standard Assets/Prefabs/Sources/Scripts/EnemyHealthScript.js:43) – Casper Stougaard Nielsen Aug 29 '13 at 18:27
  • 2
    Yeah then like I said, it's probably that `myPlayerTargetScript`, or one of its fields, is null. Without seeing your whole project that's just a guess. It might be that `myPlayerScript` doesn't initially have a `target` which would skip setting `myPlayerTargetScript` – Jerdak Aug 29 '13 at 18:55
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – LearnCocos2D Feb 16 '15 at 10:45

1 Answers1

0

Since you already hold a reference to your EnemyAI script in myPlayerTargetScript you should check this for null in OnGUI

Also don't use FixedUpdate for such things. FixedUpdate belongs to the physics system. Just use Update instead:

 #pragma strict

var healthText : GUIText;

var myPlayer : Transform;
var myPlayerScript : PlayerScript;

var myPlayerTarget : Transform;
var myPlayerTargetScript : EnemyAI;


function Start () {
    myPlayer = GameObject.FindGameObjectWithTag("Player").transform;
    myPlayerScript = myPlayer.GetComponent(PlayerScript);
}

function Update () {
    if (myPlayerScript.target != null) {
        myPlayerTarget = myPlayerScript.target;

        myPlayerTargetScript = myPlayerTarget.GetComponent(EnemyAI);
    }
}

function OnGUI () {
    if (myPlayerTargetScript  != null) {
        GUI.Label (Rect (((Screen.width / 2) + Screen.width / 3), 16, 250, 20), "EnemyHP: " + (Mathf.Round(myPlayerTargetScript.curHealth)) + " / " + myPlayerTargetScript.maxHealth);
    }
}

ps: You should tidy up your script before posting it here ;)

Bunny83
  • 490
  • 4
  • 20