Search Unity

OnGUI not working?

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

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var doorClip : AnimationClip;
    4. var doorClipReverse : AnimationClip;
    5. var Key : GameObject;
    6. var LockedDoorSound : AudioClip;
    7. var OpenDoorSound : AudioClip;
    8. var ClosedDoorSound : AudioClip;
    9. var isDoorOpen = false;
    10.  
    11. private var ChubsDoorState = false;
    12. private var ChubsDoor : ChubsKey;
    13. public var guiSkin : GUISkin;
    14.  
    15. function Start()
    16. {
    17.     ChubsDoor = GameObject.Find("ChubsKey").GetComponent(ChubsKey);
    18. }
    19.  
    20. function Update ()
    21. {
    22.     if (Input.GetKeyDown(KeyCode.E) && ChubsDoorState == true && ChubsDoor.KeyCollected == true && isDoorOpen == false)
    23.     {
    24.         GameObject.Find("ChubsDoor").GetComponent.<Animation>().Play("ChubsDoorOpen");
    25.         AudioSource.PlayClipAtPoint(OpenDoorSound, transform.position);
    26.         isDoorOpen = true;
    27.     }
    28.     else if (Input.GetKeyDown(KeyCode.E) && ChubsDoorState == true && isDoorOpen == true)
    29.     {
    30.         GameObject.Find("ChubsDoor").GetComponent.<Animation>().Play("ChubsDoorClose");
    31.         AudioSource.PlayClipAtPoint(ClosedDoorSound, transform.position);
    32.         isDoorOpen = false;
    33.     }
    34.  
    35.     if (Input.GetKeyDown(KeyCode.E) && ChubsDoorState == true && ChubsDoor.KeyCollected == false)
    36.     {
    37.         AudioSource.PlayClipAtPoint(LockedDoorSound, transform.position);
    38.     }
    39. }
    40.  
    41. function OnGUI()
    42. {
    43.     if (Input.GetKeyDown(KeyCode.E) && ChubsDoorState == true && ChubsDoor.KeyCollected == true && isDoorOpen == false)
    44.     {
    45.         GUI.skin = guiSkin;
    46.         GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 200, 100), "Opened With Key");
    47.     }
    48.     else if (Input.GetKeyDown(KeyCode.E) && ChubsDoorState == true && ChubsDoor.KeyCollected == false)
    49.     {
    50.         GUI.skin = guiSkin;
    51.         GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 200, 100), "The Door Is Locked");
    52.     }
    53. }
    54.  
    55. function OnTriggerEnter (theCollider : Collider)
    56. {
    57.     if (theCollider.tag == "Player")
    58.     {
    59.         ChubsDoorState = true;
    60.     }
    61. }
    62.  
    63. function OnTriggerExit (theCollider : Collider)
    64. {
    65.     if (theCollider.tag == "Player")
    66.     {
    67.         ChubsDoorState = false;
    68.     }
    69. }
    Focusing on the OnGUI and Update functions, why won't OnGUI display the text?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Because you're only doing anything in OnGUI when a key is down, which is for one frame.

    --Eric
     
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Ah, makes perfect sense. Thanks!