- Home /
EventSystem - detect any click NOT on object
I'm using the Unity EventSystem to detect an OnPointerUp event on my object (by implementing the IPointerUpHandler interface) which flags the clicked object as selected - the clicked object adds itself to a static variable that holds the 'currently selected' object.
I would like this object to become deselected when the user clicks anywhere other than the currently selected object i.e. the user clicks on the background, the currently selected object is deselected.
Using the Unity EventSystem, how would I achieve this?
I don't see a way of getting click data from the EventSystem without implementing one of the IPointerHandler interfaces on an actual GameObject. I know I can detect clicks with the 'standard' Input.GetMouseButton function, but this does not tell me if the user has clicked on the selected object or not.
Answer by DAVcoHL · Jul 07, 2017 at 10:02 AM
As always, I've found quite a simple solution within five minutes of submitting the question; hopefully this will be of use to someone.
Implement the IDeselectHandler handler interface.
When selecting an object, call EventSystem.current.SetSelectedGameObject(this.gameObject)
The OnDeselect event handler will be triggered on the previously selected object