I am trying to do what seems to be very well documented, ignore custom mouse click code if clicked while over a GUI button.
Heres what I have tried:
public EventSystem eventSystem;
void Start(){
eventSystem = EventSystem.current;
}
void OnGui(){
if (Input.GetMouseButton(0)) {
if (!eventSystem.IsPointerOverGameObject()){
//my custom code here.
}
}
}
I just get null reference errors pointing to eventSystem.IsPointerOverGameObject, even just requesting Debug.Log(EventSystem.current) I get null in console. What am I doing wrong, this looks straightforward. Using Unity 5 personal edition.
Old post, but I ran into this issue today and this was the first hit on Google, but didn’t have the solution I had so…
I had this issue when I accidently added an EventSystem component to one of my UI game objects. Removed it and everything was working again. Tested and confirmed that a second EventSystem in the scene that is not configured with cause EventSystem.current to return null.
All of a sudden, I am getting the same error (though it happened after downgrading Unity to 5.5.4p3 so it might be my mistake). Anyway, what worked for me is to delete the gameobject with EventSystem and recreate a new one. I hope this might help.
Old post but in case that some one stuck at this again, make sure that Event System is existed in your Project Hierarchy. If not, add one by Right Click on Project Hierarchy → UI → Event System.
This was happening to me in 2019.3.0f3 and it turned out the problem was because the Button component’s Navigation was set to “None”. Setting it to anything else got it working and EventSystem.current.currentSelectedGameObject no longer returned null.