Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mmangual_83 · Nov 27, 2013 at 01:36 PM · c#guigui-button

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;
     }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mmangual_83 · Nov 27, 2013 at 04:22 PM 0
Share

@lokindia cool, I will definitely give it a go. Thanks!

avatar image Spinnernicholas · Nov 30, 2013 at 12:31 AM 0
Share

$$anonymous$$nock, knock. How goes it?

avatar image mmangual_83 · Dec 03, 2013 at 05:24 PM 0
Share

I figured it out thanks!

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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");
   }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding a texture to a GUI button 1 Answer

HorizontalScrollbar not appearing in GUILayout.BeginScrollview C# 3 Answers

Getting Debug log errors when I load my scenes 2 Answers

How to remove a GUI element when button is pressed C# 1 Answer

Creating 2D art in a 3D world 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges