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?