Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Spacemarine658, Aug 15, 2014.

  1. Spacemarine658

    Spacemarine658

    Joined:
    Dec 9, 2012
    Posts:
    14
    NullReferenceException: Object reference not set to an instance of an object
    RespawnMenu.Update () (at Assets/Scripts/RespawnMenu.js:26)

    heres my code:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var lookAround01 : MouseLook;
    4. var lookAround02 : MouseLook;
    5. var charController : CharacterMotor;
    6.  
    7. var respawnTransform : Transform;
    8.  
    9. static var playerIsDead = false;
    10.  
    11. function Start ()
    12. {
    13.     lookAround01 = gameObject.GetComponent(MouseLook);
    14.     lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
    15.     charController = gameObject.GetComponent(CharacterMotor);
    16.  
    17.  
    18. }
    19.  
    20. function Update ()
    21. {
    22.     if (playerIsDead == true)
    23.     {
    24.         lookAround01.enabled = false;
    25.         lookAround02.enabled = false;
    26.         charController.enabled = false;
    27.     }
    28. }
    29.  
    30. function OnGUI ()
    31. {
    32.     if (playerIsDead == true)
    33.     {
    34.         if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40), "Respawn"))
    35.         {
    36.             RespawnPlayer();
    37.         }
    38.         if (GUI.Button(Rect(Screen.width*0.5-50, 240-20, 100, 40), "Menu"))
    39.         {
    40.             Debug.Log("Return to Menu");
    41.         }
    42.     }
    43. }
    44.  
    45. function RespawnPlayer ()
    46. {
    47.     transform.position = respawnTransform.position;
    48.     transform.rotation = respawnTransform.rotation;
    49.     gameObject.SendMessage("RespawnStats");
    50.     lookAround01.enabled = true;
    51.     lookAround02.enabled = true;
    52.     charController.enabled = true;
    53.     playerIsDead = false;
    54.     Debug.Log("Respawned Player");
    55. }
    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
     
    Last edited: Aug 16, 2014
  2. codejunkie83

    codejunkie83

    Joined:
    Aug 23, 2013
    Posts:
    2
    Where you assign to lookAround02, should the capitalization be changed for GameObject?
     
  3. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Please use Code Tags when posting code on the forums.
     
  4. Spacemarine658

    Spacemarine658

    Joined:
    Dec 9, 2012
    Posts:
    14
    thank you any tips on how to fix it?
     
    NomadKing likes this.
  5. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    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.
     
  6. Spacemarine658

    Spacemarine658

    Joined:
    Dec 9, 2012
    Posts:
    14
    Thanks I figured it out
     
    NomadKing likes this.