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 /
This question was closed Feb 05, 2018 at 09:43 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by TobyCZ · Sep 01, 2017 at 09:10 PM · uibuttoncolor change

How to change button color, highlited color, etc. ?

I need script for changing colors of buttons. I tried https://docs.unity3d.com/ScriptReference/UI.ColorBlock.html to change color of button, but it doesn't work for me. Is there another way how to use it?

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

  • Sort: 
avatar image
5
Best Answer

Answer by Hellium · Sep 02, 2017 at 08:26 AM

Here is a script you have to attach to the gameobject you want (preferably the button):

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class CustomButton : MonoBehaviour {
 
     public Button button ; // Drag & Drop the button in the inspector
     
     public void TurnRed()
     {
         ColorBlock colors = button.colors;
         colors.normalColor = Color.red;
         colros.highlightedColor = new Color32(255, 100, 100, 255);
         buttons.colors = colors;
     }
     
     public void TurnWhite()
     {
         ColorBlock colors = button.colors;
         colors.normalColor = Color.white;
         colros.highlightedColor = new Color32(225, 225, 225, 255);
         buttons.colors = colors;
     }
 }

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 Milos7 · Apr 07, 2021 at 10:29 AM 0
Share

The script is good only to correct spelling from "colros.highlightedColor" to "colors.highlightedColor" in both metod.

avatar image
0

Answer by karlaa15655 · Feb 04, 2018 at 10:54 PM

I used this: public void TaskOnClick1() { GameObject buttonclr = GameObject.Find("Button1"); buttonclr.GetComponent().color = Color.magenta; buttonclr.GetComponent().highlightedColor= Color.blue; } 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 · 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 Ghostling225 · Aug 01, 2021 at 09:53 AM 0
Share

Using GameObject.Find(string) is slow. If you want to do it this way and this method is on the button-carrying object itself, it's better to use gameObject.GetComponent<Button>(). + normalColor, highlightedColor, pressedColor or disabledColor + = someColorObject

Follow this Question

Answers Answers and Comments

121 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 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 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 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 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

Change the button target image alpha 1 Answer

Make a button selectable for controller but not interactable 0 Answers

Unity UI Button states 1 Answer

Buttons become invisible when changing color (on Button or on Image) in script -1 Answers

Unity 5.0.2f1 UI Button OnClick Function 6 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