Disable GUI?

Hello Guys, well I´ve been trying to deactivate a Gui button but I haven´t been able to do it yet. Here is an example of my problem:

if(Time.timeScale == 0) {
    // Create button.  
    if (GUI.Button (Rect (115,100,250,50), "Options")) { 
        //Here I need to disable that button to bring up the options menu 
        //Then if options is clicked I disable it. Then I create a slider:
        if(optionsOn)
            camRotation = GUI.HorizontalSlider (Rect (40, 40, 150, 80), camRotation, 0.0, 35.0); 
    }
}

How can I easily disable the Gui button to bring up some other stuff?
Can you guys give me an example?

[Edit by Berenger : Code formatting]

Well, just put an if-statement around that entire section, and use that to determine whether it should be drawn or not!

1 Answer

1

This smells like a job for a state machine - maybe something like this:

private var state = 1;
private var option;

function OnGUI(){
  if (Time.timeScale == 0){ // only shows options when timeScale is zero
    switch (state){
      case 1: // state 1 shows the first button:
        if (GUI.Button(Rect(115,100,250,50), "Options"){ // when button pressed...
          state = 2; // change to state 2...
          option = 0; // and set invalid option value
        }
        break;
      case 2: // state 2 shows the options:
        if (GUI.Button(Rect(115,100,250,50), "Option 1") option = 1;
        if (GUI.Button(Rect(115,120,250,50), "Option 2") option = 2;
        if (GUI.Button(Rect(115,140,250,50), "Option 3") option = 3;
        if (GUI.Button(Rect(115,160,250,50), "Option 4") option = 4;
        if (option > 0) state = 3; // if any option set, change to state 3
        break;
      case 3: // state 3 shows the slider:
        camRotation = GUI.HorizontalSlider (Rect (40, 40, 150, 80), camRotation, 0.0, 35.0);
        // button Done finishes the cycle and returns to state 1:
        if (GUI.Button(Rect(115,140,150,50), "Done") state = 1; 
        break;
    }
  }
}

Definitely the preferred way to handle this, as it is expandable.

hmm interesting way of going about this problem never used it before... thanks ill make sure to try it out even if this isnt my question!

I'd really prefer to use a state machine with an enum so it's easier to read, instead of having states like 0, 1, and 2 can be really confusing and hard to expand on and change later on. Use something like public enum State { first, options, slider } private State m_state; And then switch your 'm_state' variable.

@Dewald117, I used numbers only to show the basic idea. You're right: an enum is much better and elegant! From my own experience, after a few days we just can't remember anymore what the hell each state means...

Exellent, Thank you very much!!