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?
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*;*
var clr = b.GetComponent().colors.normalColor;
this.GetComponent().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*;*
var clr = b.GetComponent().color;
this.GetComponent().color = clr;
}