- Home /
 
Enable and disable a button selection
I have a GUI window rigged with buttons, that, when I play it they are shown already selected and I want them to remain unselected until the user selects them. Also, I want to know how to make a button remain disabled until all the fields in the GUI are filled up. I have the code here and hopefully you can help me.
 using UnityEngine;
 using System.Collections;
 public class myTestGUI: MonoBehaviour {
     enum Windows { window1, window2, window3 };
     Windows currentWindow = Windows.window1;
 
     string txtAge;
 
     int genderSelectIndex;
 
     string[] genderOptions          = { "Male", "Female" };
 
     void Awake()
     {
         InitializeMainWindowVariables();
     }
     /// <summary>
     /// Main function where the GUI windows are created
     /// </summary>
     /// 
     void OnGUI()
     {
         switch (currentWindow)
         {
             case Windows.window1:
                 SurveyWindow();
                 break;
             case Windows.window2:
                 //set the next window
                 break;
         }
                
     }
     
     /// <summary>
     /// Displays the background survey that the user will fill out
     /// </summary>
     void SurveyWindow()
     {
         int windowWidth  = 800;
         int windowHeight = 600;
 
         //// Make a background box
         GUI.Box(new Rect((Screen.width  / 2) - (windowWidth  / 2),
                          (Screen.height / 2) - (windowHeight / 2),
                           windowWidth, windowHeight), 
                           "My GUI");
         GUI.Label(new Rect(600, 250, 150, 100), "Are you male or Female?");
         genderSelectIndex = GUI.SelectionGrid(new Rect(600, 280, 150, 30), genderSelectIndex, genderOptions, 2);
         GUI.Label(new Rect(600, 330, 100, 100), "How old are you?");
         txtAge = GUI.TextField(new Rect(600, 355, 50, 40), txtAge, 2);
         if (GUI.Button(new Rect(1250, 690, 100, 50), "Continue"))
         {
             //SendMessage("Background Survey Button Pressed");
             currentWindow = Windows.window2;
         }
     }
     void InitializeMainWindowVariables()
     {
         txtAge = string.Empty;
 
         genderSelectIndex = 0;
     }
 }
 
              Answer by mmangual_83 · Dec 03, 2013 at 07:41 PM
I figured out the issue. At least for my scenario.
Problem 1: I was suing a selection grid which requires an index to pass in what the current value of it is. Even if I passed a 0 it would still highlight the first item in the grid since 0 refers to the first value of that items list.
The solution to problem 1: I just initialize the index values to -1 and it fixes my issue.
Problem 2: I wanted to disable a button that later becomes enabled after the user fills out the required items presented in the GUI form.
Solution to problem 2: I had to create a method called validate and use it like so
 bool validate()
     {
         if (foodIndex > -1 && satisfactionIndex > -1)
             return true;
         return false;
     }
 
               and within the GUI window you write in the following:
 public void myTestGUI()
 {
    //...GUI logic here
    GUI.enabled = validate();
    //..GUI button logic
    GUI.enabled = true;
 }
 
              Answer by lokindia · Nov 27, 2013 at 03:55 PM
Use GUI.Toggle instead of GUI.Button. Toggle is like an ON | OFF switch, which remains in its state of ON or OFF unless not clicked or tapped again. Please try it yourself, and if you need a sample code then lemme know.
Good luck!
Answer by Spinnernicholas · Nov 27, 2013 at 06:42 PM
Like lokindia use Toggles and just make sure the toggle values are initialized to false.
Also for a button to be disabled until all fields are filled, and maybe valid you could have an if-else block. It displays a box styled to look like a disabled button if the form is not filled and a function button if the form is filled.
 bool formFilled = false;
 
 void Update()
 {
   validateForm();
 }
 
 void ValidateForm()
 {
   if(filled() && valid())
   {
     formFilled = true;
   }
   else
   {
     formFilled = false;
   }
 }
 
 void OnGUI()
 {
   if(formFilled)
   {
     if (GUI.Button(new Rect(1250, 690, 100, 50), "Continue"))
     {
       //SendMessage("Background Survey Button Pressed");
       currentWindow = Windows.window2;
     }
   }
   else
   {
     GUI.BOX(new Rect(1250, 690, 100, 50), "Continue");
   }
 }
 
              Your answer