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\$
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.