Search Unity

Two GUI popups - 1st one needs to be destroyed

Discussion in 'Immediate Mode GUI (IMGUI)' started by taragon, Oct 13, 2011.

  1. taragon

    taragon

    Joined:
    Jan 23, 2010
    Posts:
    119
    I was wondering how I would work out the following situation: I have an NPC that generates a GUI depending on a particular variable. However, the following code generates both GUI's rendering at the same time... by both I mean: there's one that should appear when the variable no_coop_button is set to true and another to appear when the npc_trigger is set to true. I'm not sure how to generate these GUI's separately.
    Code (csharp):
    1. function OnGUI()
    2. {
    3.     if (no_coop_button)
    4.     {
    5.         GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));
    6.         GUI.Box (Rect(0, 0, 100, 100), "You are \n a thief");
    7.         GUI.EndGroup ();
    8.     }
    9.  
    10.     if (npctrigger)
    11.     {
    12.     GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));
    13.     }
    14.    
    15.     if (yesbutton)
    16.     {
    17.         GUI.Box (Rect(0, 0, 100, 100), "Enter \n treasure \n chest");
    18.         Destroy(this, 5.0f);
    19.     }
    20.    
    21.     else if (nobutton)
    22.     {
    23.         GUI.Box (Rect(0, 0, 100, 100), "You \n cannot \n enter");
    24.         Destroy(this, 5.0f);
    25.     }
    26.    
    27.     else
    28.     {
    29.         GUI.Box (Rect(0, 0, 100, 100), "Give inventory?");
    30.         if (GUI.Button (Rect(25, 25, 50, 30), "Yes"))
    31.         {
    32.             yesbutton = true;
    33.             Debug.Log("True");
    34.         }
    35.  
    36.         if (GUI.Button (Rect(25, 65, 50, 30), "No"))
    37.         {
    38.             nobutton  = true;
    39.             Debug.Log("No");
    40.         }
    41.     }
    42.     GUI.EndGroup ();
    43. }
    44.  
     
  2. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    The npctrigger check only outputs one line :

    GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));

    Which doesn't do anything visually, even though you said that it's supposed to show up a menu.

    The rest of your checks (yesbutton, nobutton and else) are outside the two if statements at the top. Nest them at their appropriate place.