Question by
burakalp34 · Apr 13 at 10:03 AM ·
2dui
How to get a buttons color
I have multiple buttons with different colors. I have the same functioin attached to all of their OnClicks. I want a GameObject I have in the scene to take the pressed buttons color. How can I do this?
Comment
Answer by RamaKrishna39 · May 18 at 07:46 AM
Let the function be GetColour() in below code and it takes a parameter. This work if color to button is given from Button Component in Inspector
public Button[] btn;
public void GetColour(int i)
{
Button b = btn[i];
var clr = b.GetComponent<Button>().colors.normalColor;
this.GetComponent<Text>().color = clr;
}
parameter i represents the index. we give i in inspector for Button0 -> i=0, Button1 -> i=1,..
The above code takes the buttons in array and my gameobject is text.
If you have given color to button from Image Component, this might work.
public Button[] btn;
public void GetColour(int i)
{
Button b = btn[i];
var clr = b.GetComponent<Image>().color;
this.GetComponent<Text>().color = clr;
}
Your answer
