How do you reference UI Event System from script?

4.8k Views Asked by At

I tried creating a public variable and then referencing the EventSystem object from the Inspector of the gameObject the script is attached to, but whenever I try to access a variable from EventSystems,

I get a NullReference Exception: Object reference not set to an instance of an object.

I tried finding it with GameObject.Find() but apparently I can't cast the gameObject to EventSystem.EventSystem with in-built cast.

I'm not sure what to do. I just want to access

EventSystem.currentSelectedGameObject.name

to find out which UI object in Canvas was selected.

1

There are 1 best solutions below

0
On

Here is what you want, see documentation here.

EventSystem.current

enter image description here