Add Listeners to array of Buttons

Hi, using unity 5.6 -

I have three buttons that I am attempting to instantiate with listeners in C#. In my function TaskOnClick(), I want to be able to be able to tell which button has been clicked so I can add further functionality.

I am fairly sure I am mucking up some code in the Start() function and also not sure if I need to add a bool to the TaskOnClick() function or if I should just use IF or Switch statements to parse through the clicks. I have been following this example: https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Button02 : MonoBehaviour {

    public Button[] buttons;

    private void Start()
    {
        for (int i = 0; i < buttons.Length; i++)
        {
            Button btns = buttons*.GetComponent<Button>();*

btns.onClick.AddListener(TaskOnClick);
}

}

public void TaskOnClick()
{
// how can I add a boolean here to check which button has been clicked?
Debug.Log(“you have clicked this button”);
}
}
I am wondering if something along the lines of this in TaskOnClick() might work: (obviously this is very crude…) Thank you in advance.
public void TaskOnClick() {
for (int i= 0; i < Lenght; i++) {
if (buttons[0] == true)
‘do something’…
else if(buttons[1] == true)
‘do something else’

All the answers given previously won’t work because of the closure problem.

While the idea of using a specific delegate it the way to go, you have to “capture” the value of the loop index before assigning the delegate :

 for (int i = 0; i < buttons.Length; i++)
 {
       int closureIndex = i ; // Prevents the closure problem
       buttons[closureIndex].onClick.AddListener( () => TaskOnClick( closureIndex ) );
 }

 // ...
 public void TaskOnClick( int buttonIndex )
 {
     Debug.Log("You have clicked the button #" + buttonIndex, buttons[buttonIndex]);
 }

@brobes05

Use a delegate to pass the button’s ID as an argument. Also, skip the btns variable.

for (int i = 0; i < buttons.Length; i++)
{
     buttons*.GetComponent<Button>().onClick.AddListener(delegate{TaskOnClick(i);});*

}
Then, change the TaskOnClick() signature to TaskOnClick(int id).

Thanks @AlexScott for the prompt reply.
Conceptually, the code seems like it would work, but the line:

btns.Button.onClick.AddListener(() => TaskOnClick(i));

returns an error as it won’t allow the call of ‘Button’ after ‘btns’.
I can’t seem to figure it out.

Thanks again for all the help.