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 JoshMBeyer · Dec 15, 2014 at 10:57 AM · c#guicolor

Drawing 4 GUIButton's with a forloop. Need help changing color?

I have a forloop to display 4 buttons. There is a value called CorrectAnswer. The button clicked compares its index to the CorrectAnswer int. How can I change that one button's text color but the other 3 remain white?

   //Display the answers.
                 for (int i = 0; i <= 3; i++)
                 {
                     
                     if (GUI.Button(new Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2 - 25) - (25 / 2) + i * 40,
                         buttonWidth, buttonHeight), mathList[mathIndex].Answers[i]))
                     {
                         //If correct answer...
                         if (i == mathList[mathIndex].CorrectAnswer)
                         {
                             Debug.Log("Correct");
                             GUI.skin.button.normal.textColor = Color.green;
                             GUI.skin.button.onHover.textColor = Color.green;
                             StartCoroutine(CorrectPause());
 
                             //here
                         }
                         else //if incorrect....
                         {
                             GUI.skin.button.normal.textColor = Color.red;
                             GUI.skin.button.onHover.textColor = Color.red;
                             Debug.Log("Incorrect");
                             StartCoroutine(IncorrectPause());
 
                             //here
                         }
                     }
                 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AdamScura · Dec 15, 2014 at 01:05 PM

You have to store the result during the MouseUp event, and then set the button's color during the Repaint event.

Think of it this way. When you click on a button, Unity is actually calling OnGUI once for each event. And there might be a few frames in between mouse down and mouse up, so the events might look like this...

  • MouseDown

  • Repaint

  • Repaint

  • MouseUp

  • Repaint

GUI.Button() evaluates to true during the MouseUp event, but not during the Repaint event. So you need to store the result during the button's if statement, and then use that result to display the correct style next time OnGUI is called for Repaint.

Here is the code:

     private int userAnswer = -1;
 
     void OnGUI()
     {
         // Define your styles
         var correctStyle = new GUIStyle(GUI.skin.button);
         correctStyle.normal.textColor = Color.green;
         correctStyle.hover.textColor = Color.green;

         var incorrectStyle = new GUIStyle(GUI.skin.button);
         incorrectStyle.normal.textColor = Color.red;
         incorrectStyle.hover.textColor = Color.red;

         // Loop over answers
         for (int i = 0; i <= 3; i++)
         {
             // Determine the style based on userAnswer
             // Start with the default style...
             GUIStyle style = GUI.skin.button; 
             // Correct answer...
             if (i == userAnswer && i == mathList[mathIndex].CorrectAnswer)
             {
                 style = correctStyle;
             }
             // Incorrect answer...
             else if (i == userAnswer)
             {
                 style = incorrectStyle;
             }
 
             // Present the button
             Rect rect = new Rect((Screen.width / 2) - (buttonWidth / 2), 
                                  (Screen.height / 2 - 25) - (25 / 2) + i * 40,
                                  buttonWidth, buttonHeight);
             if (GUI.Button(rect, mathList[mathIndex].Answers[i], style))
             {
                 // Store the answer
                 userAnswer = i;
 
                 // Do the coroutine
                 if (userAnswer == mathList[mathIndex].CorrectAnswer)
                 {
                     StartCoroutine(CorrectPause());
                 }
                 else
                 {
                     StartCoroutine(IncorrectPause());
                 }
             }
         }
     }

FYI: It's generally not a good idea to modify GUI.skin.button directly. Instead you should make a copy of it and feed your custom style into GUI.Button(). Otherwise you can get unpredictable results.

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change a buttons color? 2 Answers

c# - Change GUIlayout.lable colour based on logtype? 1 Answer

How do you change the size and color of a GUI Label in C#? 4 Answers

Distribute terrain in zones 3 Answers

Gui list and color 0 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