Hi,I am trying to create a crossword game with 36 buttons with alphabets on them. I want to be able to disable any of these buttons individually when I click on them but I dont know how to achieve this.
Remember that much of graphics rendering takes place as a state machine… To disable a GUI button, set GUI.enabled = false
just before drawing it, then set GUI.enabled = true
right afterwards to re-enable other controls that get drawn after the disabled button.
See documentation for more information.
→ Unity - Scripting API: GUI.enabled
All controls drawn with GUI.enabled = false
will be grayed out and inoperable.
Update with code sample. I had to fill the string
with the characters with periods, because I don’t know which alphabet has 36 letters. The english one only has 26. Hopefully you get the point.
Boolean[] BtnEnabled = new Boolean[36];
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ..........";
void OnGUI ()
{
for( int i=0 ; i<36 ; i++ )
{
GUI.enabled = BtnEnabled;
if( GUILayout.Button(alphabet) )
{
_BtnEnabled = false;
}
GUI.enabled = true;
}
}
Fascinating! I never knew about this... so you would still create a boolean for each of the 36 letters and do <pre> GUI.enabled = letterAbool; GUI.Button(Rect(...), A); GUI.enabled = letterBbool; GUI.Button(Rect(...), B); </pre> and so on...? Or is there an easier way? Greetz, Ky.
– DashaSorry, made a minor mistake - Booleans have their default value initialized to false, so you need to run through that array and set them all to true in your Start-function before you use that.
– RelatorI think this is definitely the "right" answer. Would you mind marking it as such?
– ByssheI asked a similar question before. I didn’t get a perfect solution in here. But I might provide some info that might be helpful.
© 2022 - 2024 — McMap. All rights reserved.
I was just wondering the same thing! If someone knows a way to "disable" a GUI.Button, please speak-up :)
– Interlace