Null Reference with UI

Hi I’ve seen a few discussions with people having the same issue of having a null reference exception when implementing:

if (InteractWithUI()){ return; }

private bool InteractWithUI()
        {
            return EventSystem.current.IsPointerOverGameObject();
        }

Such as in:

but I don’t understand the solution suggested, I also don’t fully understand what this code is even doing.

Can someone please break down what this code is doing and what is causing this exception including how to fix it.

EventSystem.current is a convenience method…

public EventSystem current => FindObjectOfType<EventSystem>();

As the great and wise Morpheus once said to Mr. Anderson, “Without a mind, the body cannot live.”. There has to be an EventSystem existing within the scene or EventSystem.current, or more to the point FindObjectOfType<EventSystem>() will return null. Instant NRE.

It’s easy to forget about the EventSystem because whenever we add a Canvas to the scene via the context menu, if there are no EventSystems in the scene, then one is automatically added to the scene, but when we import the Core object with the Canvas already on them, this logic is bypassed.

Thank you I somewhat understand the importance of the event system now, but even after checking the documentation for IsPointerOverGameObject I don’t understand what that is doing.

Also when I click to go to the current version it’s no longer there so I don’t know if the NRE I’m experiencing is because of using unity 2021.3.

The same is true for the documentation for the eventsystem itself.

Afrer reading this documentation and your response a few more times I think the issue is that there isn’t an event system in my scene because the core object being imported which doesn’t have one attached to it?

I’ve found some code on StackOverflow

and checked it in awake which has fixed the issue:

 if (FindObjectOfType<EventSystem>() == null)
        {
            var eventSystem = new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule));
        }

Is there a better alternative to this I also don’t understand why the person also added:
typeof(StandaloneInputModule));

If you add an EventSystem to your scene, either automagically when you create a Canvas or adding it explicitly, you’ll see that the GameObject with the EventSystem has a StandaloneInputModule. The two components work hand in hand.

I find this is one of the most poorly named components in Unity. It should really be named IsPointerOverUI(). This will return true only if the mouse (or if you’re using Touch input, the specific touch reference) is over a piece of UI whose canvas has a Graphics Raycaster, and which has the Raycast Target button checked, usually in an image or text component.

1 Like

I’ve never created a gameobject with the params type before does each additional type off add the components to the gameobject that’s created? If so that will make sense to me now.

That makes sense thank you.

That’s exactly what it does. I am not a huge fan of this method of instantiation, generally preferring prefabs.

How would you recommend I fix it using a prefab or should I just stick with the code?

In the case of the RPG game, I put mine in the Core prefab. Without the Core, there is no game, it has to be in every scene.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms