Search Unity

How to use GUI.enabled?

Discussion in 'Editor & General Support' started by shabazzster, Jul 21, 2011.

  1. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    I wish to disable ONE particular button when a condition is satisfied.. Just One button, not the entire gui thats connected in this window, just one button. Is that possible?

    Code (csharp):
    1.  
    2.  
    3.          
    4.             if(GUI.Button(new Rect (240,260,25,20),"+"))
    5.        
    6.             {
    7.                 convertAmount +=5;                                     
    8.                             currentAmount -=5;              
    9.                 if(currentAmount < 0) currentAmount = 0;
    10.                
    11.             }    
    12.              
    13.              else
    14.  
    15.              {
    16.                 if(currentAmount< 5) GUI.enabled = false;
    17.              }
    18.                  
    19.              
    20.         //I just want to disable this button, and no other button.
    21.  
    22.  
    Thanks
    ~K
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The docs seem to explain it quite well.

    Code (csharp):
    1. GUI.enabled = false;
    2. // button code
    3. GUI.enabled = true;
    --Eric
     
  3. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    Hi Eric!

    Yeah, I see the docs, and I see other examples but I don't see a proper example that would allow a button or anything else to disabled from am if statement- or rather, a buttoned statement wrapped within an if statement, so that when i press my button another button is disabled.

    An if you think the toggle button example has any practical value then ........????

    LOL..

    I don't see that "most practical and fundamental" example.

    Can you help me?

    The code you displayed above is what i have been using, but how do I implement it, how does it fit in a simple example?
    When I used GUI.enabled true/false, my entire GUI layout(for that component) was disabled, but i just want 1 button within the layout to be disabled.

    How do I disable a button by pressing my intended button? (not disable a component, but disable a button)

    Thanks Again
    ~K
     
  4. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    183
    Add a boolean toggle as an additional conditional. Done.

    Try the Scripting sub-forum next time.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm not clear as to how the example in the docs isn't practical; it seems to be exactly what you're asking for.

    --Eric
     
  6. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
  7. shabazzster

    shabazzster

    Joined:
    Mar 17, 2010
    Posts:
    104
    If its so practical then how bout somebody throws some code up and show me how practical it is..

    If yall say things are easy then be the man and demonstrate folks.
     
  8. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    You want to disable a different, second button from that first button? Try this:

    Code (csharp):
    1.  
    2. bool enableOtherButton = true;
    3.  
    4. if(GUI.Button(new Rect (240,260,25,20),"+"))
    5.        
    6.             {
    7.                 convertAmount +=5;                                     
    8.                             currentAmount -=5;              
    9.                 if(currentAmount < 0) currentAmount = 0;
    10.                                 enableOtherButton = true;
    11.                
    12.             }    
    13.              
    14.              else
    15.  
    16.              {
    17.                 if(currentAmount< 5) enableOtherButton = false;
    18.              }
    19.                  
    20.              
    21.         //I just want to disable this button, and no other button.
    22.                 GUI.enabled = enableOtherButton;
    23.                 if(GUI.Button(etc, etc, all the code for your second button..... )                
    24.                 GUI.enabled = true;
    25.