0
\$\begingroup\$

I have 4 buttons, which on clicking,all of them does the same thing (each displays a different string read from an array to the button's text field). I want to have a single OnClick function to keep my code clean, instead of having 4 different functions for 4 different buttons. But I need to determine which button was pressed, How to do that inside OnClick Function?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You can use EventSystem.currentSelectedGameObject defined in the UnityEngine.EventSystems namespace :

 public void OnClick()
 {        
     Debug.Log( UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name ) ;
 }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .