Search Unity

Why the NullReferenceException?

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

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    THE DOOR CODE
    Code (JavaScript):
    1. var doorClip : AnimationClip;
    2. var doorClipReverse : AnimationClip;
    3. var Key : GameObject;
    4. var LockedDoorSound : AudioClip;
    5. var OpenDoorSound : AudioClip;
    6. var ClosedDoorSound : AudioClip;
    7. var isDoorOpen = false;
    8.  
    9. private var DoorState = false;
    10. private var ChubsDoor : ChubsKey;
    11.  
    12. function Start()
    13. {
    14.     ChubsDoor = GameObject.Find("ChubsKey").GetComponent(ChubsKey);
    15. }
    16.  
    17. function Update ()
    18. {
    19.     if (Input.GetKeyDown(KeyCode.E) && DoorState == true && ChubsDoor.KeyCollected == true && isDoorOpen == false)
    20.     {
    21.         GameObject.Find("ChubsDoor").GetComponent.<Animation>().Play("chubsDoorOpen");
    22.         AudioSource.PlayClipAtPoint(OpenDoorSound, transform.position);
    23.         isDoorOpen = true;
    24.     }
    25.     else if (Input.GetKeyDown(KeyCode.E) && isDoorOpen == true)
    26.     {
    27.         GameObject.Find("ChubsDoor").GetComponent.<Animation>().Play("chubsDoorClose");
    28.         AudioSource.PlayClipAtPoint(ClosedDoorSound, transform.position);
    29.         isDoorOpen = false;
    30.     }
    31.  
    32.     if (Input.GetKeyDown(KeyCode.E) && DoorState == true && ChubsDoor.KeyCollected == false)
    33.     {
    34.         AudioSource.PlayClipAtPoint(LockedDoorSound, transform.position);
    35.     }
    36.  
    37.  
    38. }
    39.  
    40. function OnTriggerEnter (theCollider : Collider)
    41. {
    42.     if (theCollider.tag == "Player")
    43.     {
    44.         DoorState = true;
    45.     }
    46. }
    47.  
    48. function OnTriggerExit (theCollider : Collider)
    49. {
    50.     if (theCollider.tag == "Player")
    51.     {
    52.         DoorState = false;
    53.     }
    54. }
    THE KEY CODE
    Code (JavaScript):
    1. var TheKey : GameObject;
    2. var buttonInRange;
    3. var buttonActivated;
    4. var keySound : AudioClip;
    5. var KeyCollected = false;
    6.  
    7. public var guiSkin : GUISkin;
    8. private var hasPlayed = false;
    9.  
    10. function OnTriggerEnter (theCollider : Collider)
    11. {
    12.     if (theCollider.tag == "Player")
    13.     {
    14.         buttonInRange = true;
    15.     }
    16. }
    17.  
    18. function OnTriggerExit (theCollider : Collider)
    19. {
    20.     if (theCollider.tag == "Player")
    21.     {
    22.         buttonInRange = false;
    23.     }
    24. }
    25.  
    26. function OnGUI()
    27. {
    28.     if(buttonInRange == true)
    29.     {
    30.         GUI.skin = guiSkin;
    31.         GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 120, 50), "Grab Key");
    32.     }
    33. }
    34.  
    35. function Update()
    36. {
    37.     if(buttonInRange == true)
    38.     {
    39.         if(Input.GetKeyDown ("e"))
    40.         {
    41.             if(!hasPlayed)
    42.             {
    43.                 AudioSource.PlayClipAtPoint(keySound, transform.position);
    44.                 hasPlayed = true;
    45.                 KeyCollected = true;
    46.                 Destroy(TheKey);
    47.             }
    48.         }
    49.     }
    50. }
    51.  
    Can anyone tell me what it is having trouble referencing? I think I have all the right things attached to the GameObject, just let me know what it should be just to confirm. Thanks!
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    What does the error message say? It will tell you the line number of exactly where the problem is.
     
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    On ChubsDoor 16 and 21