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 michellejean · Sep 23, 2014 at 07:59 PM · guibuttoncolorunity4.6

Change the Colour of UGUI button in script?

For the new UGUI button. I want to change the colour when the button has been pressed and then when a new button is pressed I want to be able to change the colour of new button and reset the old button's colour - kind of like a radio button. However I don't know how to access the UGUI button colour in script.

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
2

Answer by Freaking-Pingo · Sep 23, 2014 at 08:07 PM

You need to add:

 using UnityEngine.UI;

In the top of your script. Then you can access all the UI related classes.

 public Button button;
 
 public void Start()
 {
    button.colors.normalColor = new Color(0.22f, 0.22f, 0.22f, 1f);
 }

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 michellejean · Sep 24, 2014 at 03:20 AM 0
Share

Ok So Obviously I'm still missing something because I had all of my buttons listed as GameObjects since I couldn't add the listeners properly when they were Buttons. So this is what I have:

 GameObject button;
 button_1 = GameObject.Find ("button_1");
   button_1.GetComponent().onClick.AddListener(delegate{selectRollNumber(1);});
 button_1.GetComponent<Button>().colors.normalColor = new   Color(0.22f, 0.22f, 0.22f, 1f);

And I get the error "Assets/Scripts/DieRollerScript.cs(165,49): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Selectable.colors'. Consider storing the value in a temporary variable"

I'm not sure what I'm doing wrong. I expect I've over complicated something.

avatar image Louis Watson · Sep 24, 2014 at 03:27 AM 0
Share

use this:

Button button_1 for your variable.

button_1.gameObject.onClick ....

button_1.colors ...

avatar image michellejean · Sep 24, 2014 at 03:42 AM 0
Share

I ended up using this: button_1.GetComponent().image.color = Color.red; Which works but I would still like to actually change the normal color in case I am ever need too.

avatar image
0

Answer by jjkiii · Sep 24, 2014 at 03:53 AM

This is what I did to change the color of the new UI images in 4.6 beta 17, hope it helps =)

 using UnityEngine.UI;
 
     public (name of script that holds the color variable lets say Image) Image image;
     public Transform Button; (either drag the button onto this or use the Start function)
     public Color color;
     
     void Start()
     {
          Button = GameObject.Find("button");
     }
     
     void Update()
     {
          //If pressed == true then do the following:
          image = Button.GetComponent<Image>();
     
          color.r = 1; (put percent out of 1 for amount of red on button i.e. I want it all red)
          color.g = 0.1f; (with a little bit of green, 10% green)
          color.b = 0.1f; (and a little bit of blue, 10% blue)
          color.a = 0.5f; (and the alpha at 50%)
     
          image.color = color;
     }
     
 
 

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

Changing gameobject material color makes object disappear from view. How can I fix this? 1 Answer

Is this code enough to crash Unity? 1 Answer

Change the color of the Unity 4.6 buttons in code 2 Answers

guiTexture color half alpha White? 1 Answer

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


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