How would I detect if a GUI button was clicked, in code?

I’m having a little issue with my inventory system which consists of 35 different slots.
Each slot is a button and when that button is clicked I want my code to set the currently selected slot in my Inventory code to be the InventorySlot component of that button.

However, using the new UI system it would be very painful going through every single button in the inspector and assign all of them to a function.

So I’m wondering if there is any way to for example do this :

ListOfInventoryButtonSlots = CreateAllButtons()

for every button in ListOfInventoryButtons, check if it is pressed;

if it is, get the InventorySlot component of that button and set the selected slot to be that component.

Hope this makes sens,e if not please comment and I’ll try to explain again. :stuck_out_tongue:

You could solve this using a delegate (which is essentially an unamed function) inside the onClick listener of a button inside a for loop. Like so:

 private void HookUpButtons()
    //Create all buttons somewhere else
    for(int i=0;i<itemButtons.Count;i++)
    var currentIndex = i;


private void InventoryItemClicked(int index)
//obviously do some null checks and index checks here
var itemIs = itemButtons[index].GetComponent();