EventSystem Current Selected Gameobject is null on click?

So I’ve made a gui button in a canvas and the OnClick() function runs a method in a script to output in the console the current selected gameobject:

Code (CSharp):

  • public void AssignHotkeyContent(){
  • Debug.Log (eventSys.currentSelectedGameObject);
  • }

And eventSys is the Event System object that the canvas is using. Now, I have another canvas (an inventory, if you will) in which you select the items (buttons) and the ‘current selected gameobject’ IS the item. So it does work… just why is it not working here? It returns null for pointer down, pointer enter, everything. Is it because it’s a different canvas or what? It’s not set to be null in any other script; and the only time my script references it is in the code above and when it sets a field ‘selectedItem’ to be the currentSelectedGameObject.

The main reason I’m trying to do this is so that when I press that button, I know which button has been pressed. An alternative method is to alter the parameter of AssignHotkeyContent(GameObject thisButton) through code but… I have no idea how to do this. Basically assign the parameter field in the inspector to be the instance of the button itself; I would just drag in the object from the hierarchy, however these buttons are instantiated at run time (for reasons). So, how do I assign the parameter of that method in script as I’m instantiating it?

All I can get is Button[0].onClick but there’s no following method that allows you to access the parameters of the events. Is there no Button[0].onClick.GetPersistantEvent(0).parameter = Button[0]?

I’m not sure if this was your issue, but mine was having more than one EventSystem component floating around in my scene.