Disable a button
Asked Answered
S

2

0

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.

Socinus answered 28/1 at 20:12 Comment(1)

I was just wondering the same thing! If someone knows a way to "disable" a GUI.Button, please speak-up :)

Interlace
R
0

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;
    }
}
Relator answered 28/1 at 20:19 Comment(3)

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.

Dasha

Sorry, 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.

Relator

I think this is definitely the "right" answer. Would you mind marking it as such?

Bysshe
S
0

I asked a similar question before. I didn’t get a perfect solution in here. But I might provide some info that might be helpful.

Socinus answered 30/8, 2011 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.