Enable / Disable GUI Components

How can I disable GUI components, i.ex. a textfield, so the user cant modify the text?

I am wasting a lots of time again looking for simple things :((.. The GUI tutorials or docs are really too basic. So i am happy for any answer, even if the answer is that it isnt possible.

Thanks.

Many hours later :((..
Apparently the GUI-System works like a state machine.
So if I want to disable a button / textfield
I simply do:

1.) GUI.enabled = false;
2.) GUI.TextField(...);
3.) GUI.enabled = true;

It would have been great to read that somewhere.
Is this the right way to do it, or is there a better way?

5 Likes

That's the way to do it. I'm not sure whether the topic of enabling/disabling is explicitly covered, but in case you haven't read it check out the GUI Scripting Guide.

In a GUI Screen where I have many Buttons. Is it possible to disabled only a few of them. can I access them with any button NAME type.
For Example:
BUY CAR
SELL CAR
CONTINUE
I want to disabled SELL CAR and CONTINUE unless the user has bought any car or any car is present in the database profile of the user. when he selects any car then i want to enable CONTINUE.
Thanks.


You can't access GUI objects by name for this purpose so you'll have to adapt the same technique as above. One way to go might be for you to have variables (buyEnabled, sellEnabled, continueEnabled) and then you toggle those values as needed and then...

GUI.enabled = buyEnabled;
// Draw your buy button here

GUI.enabled = sellEnabled;
// Draw your sell button here

GUI.enabled = continueEnabled;
// Draw your continue button here

GUI.enabled = true; // turn the GUI back on to be sure

Something like that should work well enough. :)

Where exactly are you supposed to type this GUI.enabled = false; line. My GUI elements refuse to go away from it (via a button for example). Need more about this in the GUI tutorials big time.

You precede each particular button or other control with your state machine driving whether it's enabled or not. Higgy shows this in his example.

[quote]
Set this value to false to disable all GUI interaction. All controls will be draw semi-transparently, and will not respond to user input.
[/quote]

From the Unity manual: the controls won't 'go away.' If that's what you desire, you need to specifically drive that with a state machine, and not simply whether the control can be clicked on or not.

Look a couple threads down from you, for someone who had a similar question.

http://forum.unity3d.com/viewtopic.php?t=40802


Well for starters, have you read the docs about GUI.enabled so you know what it does? I ask that as you seem to want it to cause things to "go away" and that is specifically not what it does. From the docs:

[quote]
Set this value to false to disable all GUI interaction. All controls will be draw semi-transparently, and will not respond to user input.
[/quote]

Note the bit where it notes that they'll draw, but semi-transparently. So GUI.enabled won't make anything go away. If you want that to happen then you need to track variables about whether to show something or not and then do something like this:

var ShowButton = true;

function OnGUI () {

  if (ShowButton) {
    // draw your button here
  }

}

Does that help?

Thanks it did.

Awesome, rock on!

[quote=“anon_83017716”, post:2, topic: 393262]
Many hours later :((…
Apparently the GUI-System works like a state machine.
So if I want to disable a button / textfield
I simply do:

1.) GUI.enabled = false;
2.) GUI.TextField(…);
3.) GUI.enabled = true;

It would have been great to read that somewhere.
Is this the right way to do it, or is there a better way?
[/quote]

That was very helpful, thanks for answering to your own thread :stuck_out_tongue:

[quote=“anon_85768832”, post:8, topic: 393262]
Well for starters, have you read the docs about GUI.enabled so you know what it does? I ask that as you seem to want it to cause things to “go away” and that is specifically not what it does. From the docs:

Note the bit where it notes that they’ll draw, but semi-transparently. So GUI.enabled won’t make anything go away. If you want that to happen then you need to track variables about whether to show something or not and then do something like this:

var ShowButton = true;

function OnGUI () {

  if (ShowButton) {
    // draw your button here
  }

}

Does that help?
[/quote]
how can I apply this script for all my GUI components ???

Do it with EditorGUI.DisabledScope and "using" keyword.

Example:

var selected = Selection.gameObjects;
bool noObjectSelected = selected.Length == 0;
using (new EditorGUI.DisabledScope(noObjectSelected))
{
    if (GUILayout.Button("Snap to grid")) Snap(selected);
}
1 Like