Retrieve the button that was pressed in the UI at runtime.

I have a toolbar; which has multiple buttons.

The idea is that when a button is pressed on this toolbar, the others won’t be active. The problem is that I can’t just disable the whole canvas group, since I need the button pressed to be active, so it can be pressed again to de-activate its functionality.

Is there a way at runtime, to catch the ID of the button, so I can write in the funciton, to disable all the buttons except that button? It is a lot of work, but sadly I can’t see a better way to achieve what I want. The canvas group does not discriminate between elements, so I can’t say “disable all but this element on this canvas group”.

maybe with ToggleGroup? http://docs.unity3d.com/ScriptReference/UI.ToggleGroup.html

Sadly not.

The toggle gorup works only with toggles objects; from what I can understand. Unless there is an equivalent for buttons, I don’t think that I can use that .

I did try to pass the name of teh button as delegate but this would result in me changing a lots of code. The only other alternative is to change the method signature to include also the gameobject.name, but it still ask me to add the parameter in the inspector; I can’t find a way to send with a click event, also the name of the pushed button (I believe VB .NET was able to give you the sender, in a click event, I guess the trick was never integrated in C#)

this could help, eventsystem returns currentSelectedGameObject:
http://answers.unity3d.com/answers/1099919/view.html

1 Like

Thanks mgear!

That one works like a charm. I can save in my function the name of the button now.

The trick is to add the import statement in the class file, to use EventSystem. Thanks again!