How to write a function that detects if one of several buttons was pressed?

I’m trying to make a reusable “options” pop-up window. I’m using a Panel that has 5 buttons.

I’m envisioning reusing this UI Element whenever the player has to select different options in the game, like if I give a list of names then the player can click on a name, or a list of responses, or a list of items to pick from.

So I need a function that just returns what button was clicked. I can’t tie a specific function, e.g. “do this action when this button is pressed” to the buttons because I want to be able to reuse the Panel for many different functions depending on the situation.

I’m trying to write a function that will detect which button was clicked by the user and return an int that corresponds to that button. The int will then be used to indicate which option was chosen, and a different function will act on that int.

Something like:

public int selectButton()
{
   if (button0 pressed)
     return 0;
   if (button1 pressed)
     return 1;
  ... .etc
}

I have a UIManager script and populated it with each button in the inspector.

I’m pretty new to coding/unity, and this has me stumped.
I’ve looked at the EventListener stuff, but I’m not sure if that is what I need? Doesn’t the button.OnClick in the inspector already provide that function?
Is there an easier/better way to do this than writing a separate functions for each button?
e.g:

public int clickedButton0()
return 0;

public int clickedButton1()
return 1;

etc...

That will do what I need it to do, but there must be a cleaner way?

So first up, yes you can use the event listener to add listener functions to buttons while the programm is running. This can be used like this:

  leftButton.GetComponentInChildren<Button>().onClick.AddListener(clickedButton0);

Remember to also remove listeners when the function should not be called anymore:

   leftButton.GetComponentInChildren<Button>().onClick.RemoveListener(clickedButton0);

However there is a slightly more intuitive way to handle this so long as you have a fixed set of buttons.

This can be done by creating 2 classes like this. For this we make use of the System.Action which basically is a variable type to save a function which in return can be called from this variable again.

public class ChoiceWindow : MonoBehaviour
{
    //assign buttons 1 - X here.
    public GameObject[] Buttons;
    private ButtonWrapper[] buttonWrappers;

    void Start()
    {
        buttonWrappers = new ButtonWrapper[Buttons.Length];
        for(int i=0;i< buttonWrappers.Length;i++)
        {
            buttonWrappers *= new ButtonWrapper(*

Buttons*.GetComponentInChildren(),*
Buttons*.GetComponentInChildren());*
}
}

public void setValuesForButton(int index, string text, System.Action function)
{
buttonWrappers[index].setButtonValues(text, function);
}

private class ButtonWrapper
{
Button buttonRef;
Text buttonText;
//Button function variable. We assing the function that the button should into this:
private System.Action buttonFunction;

public ButtonWrapper(Button butt, Text buttText)
{
butt.onClick.AddListener(clickedButton);
buttonRef = butt;
buttonText = buttText;
}

private void clickedButton()
{
Debug.Log($“clicked Button with text {buttText.text}”);
//call the function in the variable if any exists:
buttonFunction?.Invoke();
}

public void setButtonValues(string text, System.Action function)
{Debug.Log($“set Button text: {buttText.text}”);
buttonText.text = text;
buttonFunction = function;
}
}
}
The first one is a script to attach to your ChoiceWindow.
For example let us assume you have some function
public void AdvanceStoryToNextSomething()
{ // do stuff }
Then you can now set this function to a button by
windowRef.GetComponent().setButtonValue(0, “click to advance Story to next something”, AdvanceStoryToNextSomething);
Then this should be called when you click the first button.
Warning: This is not tested. Also no functions check if the values that you passed are valid. (e.g. you can currently pass “null” as function and this will not complain - nothing would happen)
Also i am not sure if it gets the text and button components correctly from the button gameobjects.
Let me know if that helped or if anything is unclear/there are questions to this.