Get the name of button which has been clicked

Basically, I want to apply some actions on a button which has been clicked.

I don’t want to make an individual function for each button as I have quite a lot of them.

So, How do I retrieve button’s name when I click on it?

Thanks in advance!

After playing around with this, I found that you can write the function as taking a GameObject argument. Then when you assign the function in the inspector, it lets you put in the GameObject in the slot in the inspector. That will get sent to the function, and then you can get the name from that in the function.

It just requires you drag the GameObject for each button into the inspector slot, which, if you have a lot of buttons, may be somewhat annoying. Maybe there’s a better way that I’m unaware of!

1 Like

maybe derive from StandaloneInputModule then copy and paste source code of it and add UnityEvent field for each event you need to catch globally, insert UnityEvent.Invoke(EventSystem.gameobjectSelected.name or without .name if u need serious proccessing on GO) (or so dont remember correct name) where events are fired, finally in editor insert GO with script which process ur clicked GO/GO’s name

my description is a bit mess but i think you could take correct direction with my suggestion

If you still have to drag the GameObject into the inspector for each button, then is that much different from the way I used?

You need do it only once or two no matter how many button u have u need to drag only go with script which will operate on sent ugui go

Ah. Well I suppose that would work if you want a particular GameObject sent to each function. But what if you want to send the GameObject that the button that was clicked belongs to (so for each button it’s different)? That’s the situation I’m in at the moment.

InputModules process event firing and due to this fact - in InputModule you have to know which GO was affacted by pointers. Thanks to that you could easily modify them to send clicked or whatever you want to process GO into main GO with “postprocessor” script. After this modification ur one particular GO (settled in editor) with script which contain method with GO parameter will operate on sent uGUI GO. look into source code and use ur creativity+documentation…
https://gist.github.com/stramit