- Home /
The question is answered, right answer was accepted
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?
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;
}
}
The script is good only to correct spelling from "colros.highlightedColor" to "colors.highlightedColor" in both metod.
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
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