Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

EventSystem Current Selected Gameobject is null on click?

Discussion in 'Scripting' started by JanDawid, Nov 3, 2015.

  1. JanDawid

    JanDawid

    Joined:
    Jul 7, 2014
    Posts:
    283
    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):
    1. public void AssignHotkeyContent(){
    2. Debug.Log (eventSys.currentSelectedGameObject);
    3. }
    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]?
     
    Last edited: Nov 3, 2015
  2. eyeballTickler

    eyeballTickler

    Joined:
    Jun 13, 2015
    Posts:
    7
    I'm not sure if this was your issue, but mine was having more than one EventSystem component floating around in my scene.