Triggers. Creating and attach to objects

SOS! I’m already going crazy. Maybe someone will help me to solve the problem:
buttons are created from the prefab (in the cycle). I need attach trigger “OnSelect” to each button when creating. I have an error at string “instPans*.GetComponent ().triggers.Add(new EventTrigger.Entry {callback = t, eventID = EventTriggerType.Select});” Please! Help me!!! Thank you in advance!*
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System;
public class SnapScrolling : MonoBehaviour
{
public int panCount;
public GameObject panPrefab;
public ScrollRect scrollRect;
public GameObject[ ] instPans;
public EventTrigger.TriggerEvent t;
private void Start ()
{
instPans = new GameObject[panCount];
for (int i = 0; i < panCount; i++)
{
instPans = Instantiate(panPrefab, transform, false);
var t = new EventTrigger.TriggerEvent ();
t.AddListener (data =>

{
var evData = (PointerEventData)data;
data.Use ();
OnSelectButton ();
instPans .transform.position = evData.position;
});

instPans*.GetComponent ().triggers.Add(new EventTrigger.Entry {callback = t, eventID = EventTriggerType.Select});*
//Null Reference exception
if (i == 0) continue;
}
}
public void OnSelectButton()
{
Debug.Log(“Trigger Select”);
}
}

3489060–277798–Scroll_5_modified.zip (159 KB)

Please format your code

> I have an error

Amazing! Thats a great start, and it will explain exactly what is wrong. It is very important to read the error and try to understand it. At the very least when asking for help, post your error message.

Ok! I upload a cs

3489184–277811–SnapScrolling.cs (1.02 KB)

Please format your code, dont upload the file. Just read that thread.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System;

public class SnapScrolling : MonoBehaviour
{
    public int panCount;
    public GameObject panPrefab;
    public ScrollRect scrollRect;
    public GameObject[] instPans;
    public EventTrigger.TriggerEvent t;


    private void Start ()
    {
        instPans = new GameObject[panCount];
        for (int i = 0; i < panCount; i++)
        {
            instPans [i] = Instantiate(panPrefab, transform, false);
            var t = new EventTrigger.TriggerEvent ();

        t.AddListener (data =>
              
            {
                    var evData = (PointerEventData)data;
                    data.Use ();
                    OnSelectButton ();
                    instPans [i].transform.position = evData.position;
            });

            instPans.GetComponent<EventTrigger>().triggers.Add(new EventTrigger.Entry { callback = t, eventID = EventTriggerType.Select });  //Null Reference exception
            if (i == 0) continue;
            }
        }


    public void OnSelectButton()
    {
        Debug.Log("Trigger Select");
    }


}

“Cannot Resolve symbol GetComponent”

You’re calling GetComponent on an array instead of an element in the array.

Also the reuse of the variable name “t” is probably a bug, and this line:

if(i == 0) continue;

doesn’t do anything.

Thanks, i changed instPans to instPans [i] ,

instPans[i].GetComponents<EventTrigger>().triggers

but the exception occured: “the request is not supported by the protocol version implemented by the debuggee