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
6
Question by RandomUser123 · Sep 04, 2014 at 09:35 PM · uibuttoncolorunity4.6

Change the color of the Unity 4.6 buttons in code

I'm looking to change the color of the buttons in my script, I would like the button to bed red when its not active and green when it is. I tried changing the color directly accessing the button itself but the editor told me to store it in a temporary variable. I have this so far:

         var temp = button.colors.normalColor;
         temp = Color.red;
 
         if (button.enabled == true)
         {
             temp = Color.green;
         }
         else
             temp = Color.green;

This doesn't change the button in my scene though, it stays the same default color. Can anyone lend a hand?

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

2 Replies

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

Answer by RandomUser123 · Sep 04, 2014 at 10:00 PM

Managed to figure it out, I was using the wrong properties. Instead of `button.colors.normalColor, you use the image property of the button like so:

         if (button.enabled == false)
         {
             button.image.color = Color.red;
         }

Hope this can help someone else :)

Comment
Add comment · Show 1 · 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 VectorSigma · May 16, 2015 at 04:10 PM 0
Share

Great help. Thank you!

avatar image
0

Answer by kacyesp · Sep 04, 2014 at 09:59 PM

Can you try this? Idk if you noticed but you left green as the color in both the if and the else block.

     if (button.enabled == true)
     {
         button.colors.normalColor = Color.green;
     }
     else
         button.colors.normalColor = Color.green;

The reason your original color didn't work is because you were only changing the value of temp, and not the value of normalColor. Since the type Color is a structure, values get copied over in assignments. If Color was a class rather than a structure, then temp would be a reference to normalColor and your code would have done what you expected.

Comment
Add comment · Show 4 · 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 RandomUser123 · Sep 04, 2014 at 10:01 PM 0
Share

Ha I never noticed that actually, I wasn't getting any color though unfortunately. I managed to sort it though, I was accessing the wrong properties, I have given an answer but thanks for the reply :)

avatar image vexe · Jul 16, 2015 at 03:08 PM 1
Share

You can't modify the return value of a property ('colors') so you'd have to:

 var colors = button.colors;
 colors.normalColor = some_color;
 button.colors = colors;
avatar image zapto123 vexe · Apr 23, 2016 at 04:35 PM 0
Share

Unity 5 doesn't accept the var keyword in functions, so there should be another way out

avatar image kroltan_ zapto123 · Oct 28, 2016 at 08:06 PM 0
Share

Incorrect, it does support var anywhere. Been using var everywhere since 4.5.

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

25 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

Related Questions

If a ui button is clicked, you have to move the mouse off and click to revert to normal color or animation 1 Answer

UI Button color 1 Answer

How to have correct color on imGUI Buttons (as dynamic textures) avoiding multiply effect ? 1 Answer

Change the Colour of UGUI button in script? 2 Answers

change color with playerprefs using buttons, between scenes.,How to use playerprefs to save colors from a button? 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