Hello, I must be missing something essential here. This is what I've tried:
void Start()
{
// Assign the callback to all buttons
foreach(Button subbutton in canvas.transform.GetComponentsInChildren())
{
if(subbutton.tag == "subbutton")
{
subbutton.onClick.AddListener(() => { OnSubButtonPressed(subbutton); });
}
}
}
public void OnSubButtonPressed(Button button)
{
// Shows the last button from the foreach loop, no matter what button I click.
Debug.Log("button '" + button + "' was pressed");
}
Now, the problem is that each button callback has the last button from the array (GetComponentsInChildren()) assigned as parameter, but in the foreach loop I want to attach the same callback to every button with the current button as a parameter. Is that even possible? Or is there another way to achieve that?