• 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
9
Question by mir_unity · Sep 16, 2014 at 05:26 PM · uiguicolor4.6

How to change Normal color, Highlighted color etc. in 4.6 buttons with code

The new 4.6 buttons have options for Highlighted color, Normal color, Pressed color, Disabled color. I want to change those with code.

When I try something like

 tempButton.GetComponent<Button>().colors.normalColor = Color.red;

I get an error: Assets/2-scripts/LevelSelectMenuScript.cs(32,49): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Selectable.colors'. Consider storing the value in a temporary variable

When I try something like

 tempButton.GetComponent<Button>().image.color = Color.red;

I get a weird mix of the image color (from the image component) and the normal color (from the button component)

Any ideas on how to do it right?

Comment
Add comment · Show 1
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 patch24 · Apr 28, 2015 at 11:37 AM 0
Share

If you make variables public so you can everything including ColorBlock, you can see normalColor getting changed in your script (via the inspector,) but not in the Button component itself. It's like you are basically instantiating a new ColorBlock in your script rather than getting a reference to the one on the Button component. Not understanding why this is happening. Is this a bug?

2 Replies

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

Answer by PeterMu · Sep 30, 2014 at 11:05 PM

Ahh - I had exactly this question. The hint you provided got me moving.

I found that:

 Button b = rButton.GetComponent<Button>(); 
 ColorBlock cb = b.colors;
 cb.normalColor = Color.white;
 b.colors = cb;

does the trick. (rButton is the Button game object.)

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 mir_unity · Oct 01, 2014 at 11:45 PM 0
Share

It worked. Thank you very much!

avatar image Sarseth · Oct 05, 2015 at 04:54 PM 0
Share

Ant this is so stupid! Y, it worked, but why we have to make 4 lines ins$$anonymous$$d of 1? I'm java developer, so I wonder? Cause clearly I see:

 [Serializable]
     public struct ColorBlock
     {public Color normalColor
         {
             get
             {
                 return this.m_NormalColor;
             }
             set
             {
                 this.m_NormalColor = value;
             }
         }
 }}

Why I cant use this set? Is it a unity infirmity or c#?

avatar image UrosJoksimovic · Feb 20, 2016 at 08:29 PM 0
Share

@Sarseth The reason this doesn't work (I think) is because ColorBlock is a struct and it's passed by value, not by reference, so you're essentially trying to modify a non-referenced copy of the struct.

avatar image
2

Answer by Edson · Dec 24, 2014 at 10:22 PM

Button.colors.normalColor

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Unity 4.6B UI Scrollbar Usage 1 Answer

4.6 UI Image Color.Lerp 0 Answers

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

Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers

Sorting the children of a Grid Layout in 4.6 UI 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