EventSystem.current is returning null

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.

Does your scene contain EventSystem?

Not sure, but you could be onto something. Is that something I have to add, or could have removed?

I am hitting this same issue for first time in 5.1.0f3

This worked previously

using UnityEngine.EventSystems;

	void Awake() {
		es = EventSystem.current;
}

	void Update () {
		wheel = Input.GetAxis("Mouse ScrollWheel");
		if (es.current.IsPointerOverGameObject()) {
                      ...

}

Now es is no longer assigned in Awake but no error received. I can access current directly though

i.e. this works

	if (EventSystem.current.IsPointerOverGameObject()) {

What’s changed?

check

  1. raycast component has added to main camera
  2. Eventsystem added to ur scene

Was getting this when loading a game scene asynchronously in the background of a Menu scene.
I would load Additive Async, then swap the active scenes.

Both scenes had an EventSystem object inside, and the EventSystem.current was returning the destroyed Menu scene’s version.

Fixed by brute-force iterating over game scene, and setting EventSystem.current.

foreach (GameObject newSceneRoot in newScene.GetRootGameObjects())
{
	EventSystem newEventSystem = newSceneRoot.GetComponentInChildren<EventSystem>();
	if (newEventSystem != null)
	{
		Debug.LogFormat("Loading scene {0} completed. EventSystem.current discovered and overriden.", newScene.name);
		EventSystem.current = newEventSystem;
		break;
	}
}

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 HierarchyUIEvent 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.