Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Button that destroys itself when clicked

Discussion in 'Immediate Mode GUI (IMGUI)' started by Predster, May 5, 2008.

  1. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Greetings,

    Sorry if this is a really stupid question, but I'm having problems and the scripting reference doesn't really address this. :( (n00b alert)

    At the start of my level, I want to draw a GUI box displaying text about what the player will do. I want a button in the box which, when clicked, will destroy itself and the box (" a close window" button). I am having heaps of trouble figuring out how to remove/destroy GUI elements.


    Here is my code. The button removes the box, but I don't know how to get it to remove itself! Any help is much appreciated.


    Code (csharp):
    1.  
    2. private var showDest : boolean = true;
    3.  
    4.  
    5.  
    6. function OnGUI()
    7. {  
    8.     if (GUI.Button (Rect (Screen.width/2,Screen.height/2,100,100), "Begin")) {
    9.         showDest=!showDest;     //GUI button that closes window displaying destination
    10.         }
    11.     if (showDest==true) {
    12.     GUI.Box(Rect(Screen.width/2,Screen.height/2,500,200), "Find your way to "+ destinationname);  //display GUI box at start of level which shows player their destination.
    13.    
    14.     }
    15.    
    16.    
    17. }
     
  2. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    Code (csharp):
    1. var showgui:boolean;
    2. function OnGUI(){
    3.   if (showgui){
    4.     if GUI.Button(whatever){
    5.       showgui=0;
    6.     }
    7.   }
    8. }
    9.  
    [OSRF]=(or some reasonable facsimile)
     
  3. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    And to add to that, that's the general strategy for showing or hiding any GUI layout -- use an outside condition to show the layout to begin with and then have the close button turn it off along with whatever else it needs to do.
     
  4. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    I see, thanks. I thought there might be an easier way to do it, but this works for me!