Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Silent_Eagle · Dec 21, 2016 at 07:33 AM · c#unity 5uiguicolor

How to change a buttons color?

im having trouble setting the color of a button I want when the player money is less than the invest cost then I want the button to be gray and the interaction turned off. But when I change the colors the button image completely disappears but the color of the button in the inspector is the same as the one I set in the code, just I cant see it because the Image is gone.

     public Button investButton;
     public ColorBlock investButtonColor;
 
     void Update()
     {
         if (tap.money >= investCost)
         {
             investButton.colors = investButtonColor;
             investButtonColor.highlightedColor = Color.yellow;
             investButtonColor.normalColor = Color.yellow;
             investButtonColor.pressedColor = Color.yellow;
             
 
             
         }
         else
         {
             investButton.colors = investButtonColor;
             investButtonColor.highlightedColor = Color.cyan;
             investButtonColor.normalColor = Color.cyan;
             investButtonColor.pressedColor = Color.cyan;
 
             
         }
     }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Oziry · Dec 27, 2016 at 10:24 AM

perhaps something like :

    investbutton.GetComponent<Image>().color = Color.yellow;


Comment
Add comment · Show 3 · 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 LoloMiMama · Sep 13, 2020 at 12:58 AM 0
Share

And 4 years later, you've just saved a random guy! Thank you so much. The colors property from Button component doesn't work fine at all, it is really buggy.

avatar image Babelguppy · Oct 27, 2020 at 11:55 AM 0
Share

Still saving people's butts, Oziry. Also, as a helping hand to future seekers of this knowledge, I ran across this problem when setting the colors for this using the color picker: https://answers.unity.com/questions/908438/buttoons-become-invisible-when-changing-color-on-b.html

avatar image alankarmisra Babelguppy · Oct 30, 2020 at 09:48 PM 0
Share

Actually OP had the right idea but incorrect sequence. You need to change the values in the ColorBlock first and THEN assign. The button component presumably copies these values so it's not an assignment per-say.

 investButtonColor.highlightedColor = Color.yellow;
 investButtonColor.normalColor = Color.yellow;
 investButtonColor.pressedColor = Color.yellow;
 investButton.colors = investButtonColor;

See the following thread: https://answers.unity.com/questions/1401626/how-to-change-button-color-highlited-color-etc.html

avatar image
-1

Answer by Denscrivent · Dec 27, 2016 at 09:43 AM

Try using color instead of colorblock

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 karlaa15655 · Feb 04, 2018 at 10:52 PM

I used this: public void TaskOnClick1() { GameObject buttonclr = GameObject.Find("Button1"); buttonclr.GetComponent().color = Color.magenta; } You have to find the button with you want to work with, so instead of "Button1" put the name of your button After that, you have to assign the void to the button

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

13 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

Related Questions

Getting number of seconds a user has been in game 2 Answers

UI text color not assigning correctly 2 Answers

Xbox Controller and Unity Buttons UI 0 Answers

Make image follow your cursor 1 Answer

Trouble with ZXing QR Scanning 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