Javascript error NullReferenceException: Object reference not set to an instance of an object

NullReferenceException: Object reference not set to an instance of an object
RespawnMenu.Update () (at Assets/Scripts/RespawnMenu.js:26)

heres my code:

#pragma strict

var lookAround01 : MouseLook;
var lookAround02 : MouseLook;
var charController : CharacterMotor;

var respawnTransform : Transform;

static var playerIsDead = false;

function Start ()
{
    lookAround01 = gameObject.GetComponent(MouseLook);
    lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
    charController = gameObject.GetComponent(CharacterMotor);

 
}

function Update ()
{
    if (playerIsDead == true)
    {
        lookAround01.enabled = false;
        lookAround02.enabled = false;
        charController.enabled = false;
    }
}

function OnGUI ()
{
    if (playerIsDead == true)
    {
        if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40), "Respawn"))
        {
            RespawnPlayer();
        }
        if (GUI.Button(Rect(Screen.width*0.5-50, 240-20, 100, 40), "Menu"))
        {
            Debug.Log("Return to Menu");
        }
    }
}

function RespawnPlayer ()
{
    transform.position = respawnTransform.position;
    transform.rotation = respawnTransform.rotation;
    gameObject.SendMessage("RespawnStats");
    lookAround01.enabled = true;
    lookAround02.enabled = true;
    charController.enabled = true;
    playerIsDead = false;
    Debug.Log("Respawned Player");
}

I don’t get what the error is trying to tell me I know its saying something im referencing doesn’t exist but it does

Where you assign to lookAround02, should the capitalization be changed for GameObject?

Please use Code Tags when posting code on the forums.

thank you any tips on how to fix it?

1 Like

One of your GetComponent calls is returning null, which you later then try to reference. If you select the component with this script on when the error is thrown, you should be able to see if any of your variables are empty.

Thanks I figured it out

1 Like