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.lastSelectedGameObject is always null on some objects

Discussion in 'Scripting' started by Harito, Jan 12, 2015.

  1. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Hello, I recently updated to 4.6.1 from one of the betas, I can't exactly remember which one (18-20?) and after the update, inside the callback of some button's onClick event, EventSystem.lastSelectedGameObject is always null... Nothing was changed in the code or the scene form before I updated, it worked before just fine.

    I receive the event in my callback ( all of the buttons' callbacks are set dynamically ), but lastSelectedGameObject is always null.

    Even when creating new buttons, with a simple Image component, i still get lastSelectedGameObject to be null. Events fire fine, but always with a null reference to lastSelectedGameObject or currentSelectedGameObject.

    I then tried switching to EventTriggers, but the same thing happened. Callback called just fine, but
    BaseEventData.selectedObject as well as EventSystem.lastSelectedGameObject are still null.

    Does anyone know if there's a gotcha popped up since the beta that I'm missing? Can't seem to find any other way to refer to the object that was just clicked...

    Thanks!
     
    Last edited: Jan 12, 2015
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Can't you just use Selection.gameObject?
     
  3. Harito

    Harito

    Joined:
    Mar 3, 2014
    Posts:
    22
    Selection.gameObject is for the Editor. I'm talking about the runtime engine, with uGUI. :)

    I worked around it by messing with the source, ( made PointerInputModule.GetLastPointerEventData public ) but it would be nice to know why it won't work.
     
  4. kaizadavari

    kaizadavari

    Joined:
    Jan 21, 2016
    Posts:
    2
    @Harito I ran into the exact same problem as you did. Could you please explain what you did to fix it? Thanks.
     
  5. 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.