Using for loop to run code for each Button in a list

Hey all,

I’m very new to C# and was trying to create a system that gets all of the Buttons in a list and applies the same code to all of them without me having to copy and paste the code who knows how ever many amounts of time

My question is, In order for this to work I need to apply the code to the next Button in the list, then move on until there are no more anymore. This is what I have:

 void Start()
    {
        AddUIEvents();
    }

    void AddUIEvents()
    {
        List<Button> Buttons = new List<Button>();

        Buttons.Add(MAN_UI);

        for (var i = 0; i <= Buttons.Count; i++)
        {
            int index = Buttons.IndexOf(Buttons.Count);
            int next = list[index + 1];


            Button nextButton = Buttons[next];
            EventTrigger eventTrigger = nextButton.gameObject.AddComponent<EventTrigger>();

            EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry();
            pointerDownEntry.eventID = EventTriggerType.PointerDown;
            pointerDownEntry.callback.AddListener((data) => { OnButtonDown((PointerEventData)data); });
            eventTrigger.triggers.Add(pointerDownEntry);

            EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry();
            pointerUpEntry.eventID = EventTriggerType.PointerUp;
            pointerUpEntry.callback.AddListener((data) => { OnButtonUp((PointerEventData)data); });
            eventTrigger.triggers.Add(pointerUpEntry);
        }
     
    }

MAN_UI is just a public button,

I get the following errors:
(88,41): error CS1503: Argument 1: cannot convert from ‘int’ to ‘UnityEngine.UI.Button’

(89,24): error CS0103: The name ‘list’ does not exist in the current context

Your help is very much appriciated.

Hello, what is the variable called list ? Is it your buttons list ?
Why not directly doing this :

void AddUIEvents()
{
    List<Button> Buttons = new List<Button>();
    Buttons.Add(MAN_UI);
    foreach (var button in Buttons)
    {
            EventTrigger eventTrigger = button.gameObject.AddComponent<EventTrigger>();
            EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry();
            pointerDownEntry.eventID = EventTriggerType.PointerDown;
            pointerDownEntry.callback.AddListener((data) => { OnButtonDown((PointerEventData)data); });
            eventTrigger.triggers.Add(pointerDownEntry);
            EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry();
            pointerUpEntry.eventID = EventTriggerType.PointerUp;
            pointerUpEntry.callback.AddListener((data) => { OnButtonUp((PointerEventData)data); });
            eventTrigger.triggers.Add(pointerUpEntry);
    }
}
1 Like

Thank you so much! Did not know about the foreach function, that’s going to be super useful. Could you recommended a certain course, youtuber, or website to learn these basic functions of C#?

It´s a bit hard to see the errors, cause it looks like you didn´t copy the complete script, so the lines of the error messages don´t match with the lines in the shown code.

But the error messages tell you what the problems are:
(88,41): error CS1503: Argument 1: cannot convert from ‘int’ to ‘UnityEngine.UI.Button’: IndexOf() expends an object not an int. so you have to do: Buttons.IndexOf([Buttons*);*
(89,24): error CS0103: The name ‘list’ does not exist in the current context: you never defined a field “list”, you have to use: Buttons[index + 1].
But i don´t get the code at all. Why do you do this lines:
```csharp
*int index = Buttons.IndexOf(Buttons.Count);

int next = list[index + 1];
Button nextButton = Buttons[next];*
* *you have the current index in the for loop, you can work with that. just do:* *csharp
Button nextButton = Buttons[i];
```

the loop stops automatic after the last entry of you list

Unity learn has some course available. You also can follow codecademy learning course for the basics.