EventSystem.current.currentSelectedGameObject always returns null

Setup:
Unity version: 6000.6.0b4
EventSystem is in the scene
PlayerInput is in the scene
both are working.

When I query the EventSystem for the current selected gameobject:

var go = EventSystem.current.currentSelectedGameObject

I always get a null ref. I can see the EventSystem’s selected gameobject with a button component in the inspector window.

NullReferenceException: Object reference not set to an instance of an object
[redacted].UI.EraseCards+<>c__DisplayClass19_0.<Start>b__1 () (at Assets/[redatcted]/Scripts/UI/EraseCards.cs:81)
UnityEngine.Events.InvokableCall.Invoke () (at <816930b870114204a112306c6710f86a>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <816930b870114204a112306c6710f86a>:0)
UnityEngine.UI.Button.Press () (at ./Library/PackageCache/com.unity.ugui@76e09b1c57eb/Runtime/UGUI/UI/Core/Button.cs:71)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at ./Library/PackageCache/com.unity.ugui@76e09b1c57eb/Runtime/UGUI/UI/Core/Button.cs:115)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui@76e09b1c57eb/Runtime/UGUI/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at ./Library/PackageCache/com.unity.ugui@76e09b1c57eb/Runtime/UGUI/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@76e09b1c57eb/Runtime/UGUI/EventSystem/EventSystem.cs:527)

Is this a known issue?
Is there another way I can get this information?

Use case?

For editor tools, use Selection.activeObject or similar.

For runtime, I don’t see why you should need a “selected object” from the event system. If you do some sort of selection behaviour like in an RTS you would have a class that holds this selection. If you want to find out what button is being pressed or otherwise interacted, then the button holds that information. By name, by tag, by parent, by GetComponentInParent, by custom component that gets the target object assigned.

Either way, I think the problem here is that the OnPointerClick is currently processing and at the time your callback runs it has technically not been “selected”. You’d have to kick off a coroutine that yields till the end of the frame to find out but that’s just shooting yourself in the foot. Better solutions are readily available, like literally inside:

void OnErase()
{
    var selectedObject = gameObject;
}

Or by passing the reference from the UI:

void OnErase(GameObject target)
{
    var selectedObject = target;
}

Note: the Unity Event system is terrifyingly terrible, especially when you want to write a card game with that, and on top also having to assign (and never lose) any references passed into the events from the Inspector. Instead use regular C# events because OnErase, OnDraw, OnShuffle, etc are all pretty much hardcoded events eg you click, draw, or shuffle ANY card and this invokes the event on the affected cards which pass themselves as the parameter along.

Thanks for your reply!
My use case is that in runtime, I have a list of cards in the UI. The player navigates to them with the keyboard or gamepad. They can then press a separate button which will erase the card. So I was wanting that button to know which gameobject the event manager was on. There’s a problem with my logic here in that IF this was working in the way this old documentation says it works, I’d have a race condition with pressing the button, so that part wouldn’t work any ways. (note: I can’t find the event system in current documentation.) Unity - Scripting API: EventSystems.EventSystem.currentSelectedGameObject
" The GameObject currently considered active by the EventSystem".

I’ll set up a script on the card gameobjects which sends an event to my EventListener when it is selected so that the erase button can grab the card from there.

I couldn’t find this tidbit of information anywhere else, so I’m posting it here. To access the current selected gameObject, you’ll first need to do a null check on the EventSystem.current else you’ll get null refs. ex:

if (EventSystem.current != null && EventSystem.current.currentSelectedGameObject != gameObject)
{
     do something
}