- Home /
How can I change the highlighted color of a Unity button when it's clicked?
I was playing around with buttons in Unity, and I wanted to see if I could change a button's highlighted color every time I clicked on it. I added a script in the same GameObject as a button component, and I had a changeColor
function attached to the component's OnClick(). The function was in a ButtonScript class written like this:
public class ButtonScript : MonoBehaviour {
public Button button;
public void changeColor() {
Debug.Log ("Changing highlighed color");
int r = Random.Range (0, 255);
int g = Random.Range (0, 255);
int b = Random.Range (0, 255);
Color currentColor = button.colors.highlightedColor;
currentColor.r = r;
currentColor.g = g;
currentColor.b = b;
}
}
I couldn't do button.colors.highlightedColor = new Color(r,g,b);
because I got a "Consider storing the value in a temporary variable" error. When I tried a test play and clicked the button multiple times, the highlighted color didn't change. How can I make a function that changes a button's highlighted color when it's clicked, if it's possible?
So I would do what it suggests, and store it in a temp variable - reason why is because certain things, you cant directly access because of different protection levels and things like "read only"
Your on the right track, all you need to do is apply the color. So button.colors.highlightedColor = currentColor;
and that should be all youd need to add to your code, to get the color to actually apply.
That line didn't work, oddly. Still got a "storing the value in a temporary variable error". but I was able to make it work by reassigning to button.colors
directly. Thanks for the idea though.
Answer by DragonautX · Nov 21, 2016 at 02:04 AM
I found that I needed to reassign currentColor
to button.colors.highlighted
afterwards. I couldn't do button.colors.highlightedColor = currentColor
, but I could reassign to the ColorBlock
that button.colors
returns (I got the idea from @vexe's comment here). I also saw that the Color constructor takes floats from 0 to 1, not ints from 0 to 255 (even so, should've been Random.Range(0,256)
. Here's the class that worked for me:
public class ButtonScript : MonoBehaviour {
public Button button;
public void changeColor() {
Debug.Log ("Changing highlighed color");
float r = Random.Range (0f, 1f);
float g = Random.Range (0f, 1f);
float b = Random.Range (0f, 1f);
ColorBlock colorVar = button.colors;
colorVar.highlightedColor = new Color (r, g, b);
button.colors = colorVar;
}
}
Recently I also ran into this issue. I was using the Select() method, and therefore I needed to add the following code for it to work correctly:
...
colorVar.selectedColor = new Color (r, g, b);
...
Answer by Delphoenyx · Jun 29, 2021 at 01:04 AM
@DragonautX solved my problem, which was a little bit differente, but I only needed this:
ColorBlock colorVar = button.colors;
colorVar.highlightedColor = new Color (r, g, b);
button.colors = colorVar;
(Obiously with a value for r, g b)
The problem in my case was that I wasn't able to modify directly colors.highlightedColor because Unity told me that it wasn't a variable. So I just had to modify colors directly, in the way @DragonautX showed.
Just get colors to a ColorBlock variable, modify that variable, and then assign colors' value to that variable.
I know this is not the same problem that was originally posted, but I found this question trying to fix my problem, so I hope this can help someone who ends up here just as me.
@Delphoenyx Hi, I got an email notification about your answer. What are you using Unity for? I'm just curious. I haven't used Unity in a long time. Like for example, the website says I wrote this question back in 2016. So it's interesting for me to see this again.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
UnassignedReferenceException: But wait; It's there! 0 Answers
How to control GUITextures within a single C# script? 1 Answer
Highscore GUI not working properly help 0 Answers