Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Tiebo · May 05, 2017 at 09:30 AM · unity 5uicolor changetoggle button

Toggle Button: different color for ON and OFF state?

Hey guys, trying on this for hours now. Can you tell me if there is an build in way to set a ToggleButton's color depending on it's state (on/off)? More in detail: I have a toggle group, where only one toggle can be in the on state at the time. This should be visualized by the toggle color. All off toggles white, the on toggle gray. Seems to me like the most normal thing, but I don't get it working. Thanks, Tobi

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
2
Best Answer

Answer by Hellium · May 05, 2017 at 09:30 AM

I haven't tested the following code, but you will get the idea

 private UnityEngine.UI.Toggle toggle ;
 
 void Start()
 {
      toggle = GetComponent<UnityEngine.UI.Toggle>();
      toggle.onValueChanged.addListener( OnToggleValueChanged ) ;
 }
 
 private void OnToggleValueChanged( bool isOn )
 {
      toggle.color = isOn ? new Color(0.5f, 0.5f, 0.5f ) : new Color(1, 1, 1 ) ;
 }


https://docs.unity3d.com/ScriptReference/UI.Toggle-onValueChanged.html

Comment
Add comment · Show 2 · 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 Tiebo · May 08, 2017 at 02:47 PM 0
Share

Thank you very much! I don't really understand why it's not possible to select this color just like the normal and highlighted in the inspector, but your code works just fine.

I ended up doing this: For changing the toggle.color this workaround via Colorblock was necessary.

     private Toggle toggle;
 
     private void Start()
     {
         toggle = GetComponent<Toggle>();
         toggle.onValueChanged.AddListener(OnToggleValueChanged);
     }
 
     private void OnToggleValueChanged(bool isOn)
     {
         ColorBlock cb = toggle.colors;
         if (isOn)
         {
             cb.normalColor = Color.gray;
             cb.highlightedColor = Color.gray;
         }
         else
         {
             cb.normalColor = Color.white;
             cb.highlightedColor = Color.white;
         }
         toggle.colors = cb;
     }
avatar image unity_P0X207LzI7FZyA Tiebo · Feb 23, 2019 at 07:38 AM 0
Share

Hi I used your code for my toggle it was helpful, I couldn't figure out why I couldn't just tell it what color to be !

avatar image
5

Answer by StephenA · Mar 05, 2020 at 10:51 PM

I've find another simplier workaround without code.

In your Toggle's Children select CheckMark and delete the source Image, then set the color to what you want and finally in the Rect transform Stretch to fill the background.

And that's all.

Verify that in your Toggle component Graphic (under Is On) still point to your modified CheckBox

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
0

Answer by unity_Fi7ZwG8y2_j8oQ · Dec 15, 2021 at 11:27 AM

OMG, I have wasted more time coding around this until I came across your answer. So much easier and works exactly how I thought Toggles should have in the first place!

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

9 People are following this question.

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

Related Questions

How to make an UI element clickable based on its fill amount? 1 Answer

UI Slider when not interactable changes color 1 Answer

How to trigger OnValueChange(Vector 2) method from code? 1 Answer

Cannot implicitly convert 'UnityEngine.UI.Button[]' to 'UnityEngine.UI.Button' 1 Answer

Почему после перехода на другую версию Unity при создании кнопки она не откликается на геймпад и клавиатуру? 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