Search Unity

JS 19, NullReference

Discussion in 'Scripting' started by GamesOnAcid, Feb 9, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    I've searched for a while now, and it seems that I am still getting this null reference after a bit of debugging.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var doorClip : AnimationClip;
    4. var Key : GameObject;
    5. var SledgehammerHit : AudioClip;
    6. var LockedDoorSound : AudioClip;
    7. var isDoorOpen = false;
    8.  
    9. private var BakeryDoorState = false;
    10. private var BakeryDoor : Sledgehammer;
    11.  
    12. function Start()
    13. {
    14.     BakeryDoor = GameObject.Find("Sledgehammer").GetComponent(Sledgehammer);
    15. }
    16.  
    17. function Update ()
    18. {
    19.     if (Input.GetKeyDown(KeyCode.E) && BakeryDoorState == true && BakeryDoor.KeyCollected == true && isDoorOpen == false)
    20.     {
    21.         GameObject.Find("BakeryDoors").GetComponent.<Animation>().Play("bakeryDoorsFalling");
    22.         AudioSource.PlayClipAtPoint(SledgehammerHit, transform.position);
    23.         isDoorOpen = true;
    24.     }
    25.  
    26.     else if (Input.GetKeyDown(KeyCode.E) && BakeryDoorState == true && BakeryDoor.KeyCollected == false)
    27.     {
    28.         AudioSource.PlayClipAtPoint(LockedDoorSound, transform.position);
    29.     }
    30.  
    31.  
    32. }
    33.  
    34. function OnTriggerEnter (theCollider : Collider)
    35. {
    36.     if (theCollider.tag == "Player")
    37.     {
    38.         BakeryDoorState = true;
    39.     }
    40. }
    41.  
    42. function OnTriggerExit (theCollider : Collider)
    43. {
    44.     if (theCollider.tag == "Player")
    45.     {
    46.         BakeryDoorState = false;
    47.     }
    48. }
    For reference, I have 2 objects called Sledgehammer and BakeryDoors. The doors have the door script, and audio source, a trigger, and an Animation with an anim attached called bakeryDoorsFalling. The animation is set to Legacy. It only occurs when I push E on the door with the hammer collected, so it has to be in the 'if' somewhere, Can anyone tell me why it's occuring?
     
    Last edited: Feb 9, 2016
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    errors generally give the line number that is causing it... which line is referenced in the error?
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    @LeftyRighty Perhaps the one that is stated in the title, I'm not sure though.

    @GamesOnAcid I assume it's caused in line 19 (due to the thread title, if not, provide more information).
    Check whether

    Code (JavaScript):
    1. BakeryDoor = GameObject.Find("Sledgehammer").GetComponent(Sledgehammer);
    returns null or finds an object. Put a Debug.Log statement and compare it to null. If it's not the one causing the issue, continue just like that.
     
    Last edited: Feb 9, 2016
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ugh, read that as the JS error code for a nul ref o_O :morecoffeeneeded:
     
  5. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Yes, when I check it, it returns null immediately on start. I do have a GameObject called Sledgehammer, and a script with the same name on it. Here is the code for it if necessary.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var TheKey : GameObject;
    4. var buttonInRange;
    5. var buttonActivated;
    6. var KeyCollected = false;
    7.  
    8. public var guiSkin : GUISkin;
    9. private var hasPlayed = false;
    10.  
    11. function OnTriggerEnter (theCollider : Collider)
    12. {
    13.     if (theCollider.tag == "Player")
    14.     {
    15.         buttonInRange = true;
    16.     }
    17. }
    18.  
    19. function OnTriggerExit (theCollider : Collider)
    20. {
    21.     if (theCollider.tag == "Player")
    22.     {
    23.         buttonInRange = false;
    24.     }
    25. }
    26.  
    27. function OnGUI()
    28. {
    29.     if(buttonInRange == true)
    30.     {
    31.         GUI.skin = guiSkin;
    32.         GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 200, 100), "Pick Up Sledgehammer");
    33.     }
    34. }
    35.  
    36. function Update()
    37. {
    38.     if(buttonInRange == true)
    39.     {
    40.         if(Input.GetKeyDown ("e"))
    41.         {
    42.             if(!hasPlayed)
    43.             {
    44.                 hasPlayed = true;
    45.                 KeyCollected = true;
    46.                 Destroy(TheKey);
    47.             }
    48.         }
    49.     }
    50. }
    51.