- Home /
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.
Answer by YoungDeveloper · May 09, 2015 at 07:47 PM
Does your scene contain EventSystem?
$$anonymous$$y scene didn't have an event system, I added it, still didn't work. I should note I'm using GUI.Layout.
Answer by BobbyDoogle · May 09, 2015 at 08:09 PM
Not sure, but you could be onto something. Is that something I have to add, or could have removed?
Answer by michela · Jun 13, 2015 at 08:46 PM
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?
Answer by MFKJ · Aug 28, 2015 at 08:20 AM
check 1. raycast component has added to main camera 2. Eventsystem added to ur scene
Answer by hbcongoz · Sep 12, 2017 at 05:19 AM
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;
}
}
In 2018.4 I see similar behaviour. If I load a scene additive and async, EventSystem.current will SO$$anonymous$$ETI$$anonymous$$ES be null in the Awake() for objects in the new scene. This happened after updating a plugin unrelated to input. It might have something to do with script execution order. For me, EventSystem.current stops being null by itself. I think this happened when my other scene finished unloading.
Your answer

Follow this Question
Related Questions
How to let handled events / raycasts pass through 1 Answer
EventSystem - all touches have the same ID 3 Answers
4.6 UI Full controller support 0 Answers
Android Touch Issue(2 buttons) 0 Answers
offset the position of a click on uGUI 0 Answers