NullReferenceException when upgraded from 2023.2.7f1 to 2023.2.17f1

Hello,
my game was using UI Toolkit in 2023.2.7f1 and it worked fine.
Now I updated to 2023.2.17f1 and got the error.
Is there someone experienced the issue?

NullReferenceException: Object reference not set to an instance of an object

UnityEngine.UIElements.BaseVerticalCollectionView.Apply (UnityEngine.UIElements.KeyboardNavigationOperation op, UnityEngine.UIElements.EventBase sourceEvent) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.KeyboardNavigationManipulator.Invoke (UnityEngine.UIElements.KeyboardNavigationOperation operation, UnityEngine.UIElements.EventBase evt) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.KeyboardNavigationManipulator.OnNavigationSubmit (UnityEngine.UIElements.NavigationSubmitEvent evt) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventCallbackRegistry+DynamicCallbackList.Invoke (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel, UnityEngine.UIElements.VisualElement target) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventDispatchUtilities.HandleEvent_BubbleUpCallbacks (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel, UnityEngine.UIElements.VisualElement element) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventDispatchUtilities.HandleEventAcrossPropagationPath (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel, UnityEngine.UIElements.VisualElement target, System.Boolean isCapturingTarget) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel, UnityEngine.UIElements.VisualElement target, System.Boolean isCapturingTarget) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventDispatchUtilities.DispatchToFocusedElementOrPanelRoot (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.NavigationEventBase`1[T].Dispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at <657b8300aeb24adfa77d48e89b1aaa3e>:0)

UnityEngine.UIElements.PanelEventHandler.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.EventSystems.BaseEventData sourceEventData) (at ./Library/PackageCache/com.unity.ugui@2.0.0/Runtime/UGUI/EventSystem/UIElements/PanelEventHandler.cs:271)

UnityEngine.UIElements.PanelEventHandler.OnSubmit (UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui@2.0.0/Runtime/UGUI/EventSystem/UIElements/PanelEventHandler.cs:211)

UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.ISubmitHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui@2.0.0/Runtime/UGUI/EventSystem/ExecuteEvents.cs:134)

UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at ./Library/PackageCache/com.unity.ugui@2.0.0/Runtime/UGUI/EventSystem/ExecuteEvents.cs:272)

UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@2.0.0/Runtime/UGUI/EventSystem/EventSystem.cs:530)

The error was printed when a GameObject with UI Document and a tiny logic script.
I suspected my logic script was doing some bad, so I commented out all the logic inside it.
The UI Document is also tiny. It has VisualElement at the top, a Label inside it.

Another GameObject has Event System and Input System UI Input Module.
I tried removing the two components from the GameObject.
Nothing changed.

I added again the two components Event System and Input System UI Input Module, this time newly created by Add Component button.
Nothing changed.

Hi,
Could you please file a bug report Unity QA: Building quality with passion

OK, I’ve now submitted a bug report with a tiny project to reproduce.
(In fact, I guess the problem is not in the UI screen where the user is going to, but in the UI screen that the user has just left. I’ll investigate more on my side, too)

1 Like

To narrow down, InputSystem is not related to the issue. The tiny project doesn’t use InputSystem, but reproduced the bug.

I found a workaround and it could be also a clue to the root cause.

There’s only one scene MenuScene in the project.
There are two GameObjects with UI Document and their own tiny logic script.
(There are other GameObjects, but they are not related to the issue)

The first GameObject involved is LanguageUI which has ListView where the user can choose a language. The List View is placed inside a VisualElement.
The other is EulaUI which has a Label inside a VisualElement.

The game starts from LanguageUI. When the user selects a languate, the game proceeds to EulaUI. That is, LanguageUI is deactivated by SetActive(false) and EulaUI is activated by SetActive(true).

LanguageUI and EulaUI are derived from ScreenUI.

public class ScreenUI : MonoBehaviour {
protected void AdvanceTo(string nextName) {
ScreenHistory.ClearBackward();
SwitchToScreen(ScreenList.Get(nextName));
}
void SwitchToScreen(ScreenUI next) {
next.gameObject.SetActive(true);
gameObject.SetActive(false);
}
}

Noe AdvanceTo() is a utility function that deactivates current GameObject and activates the next GameObject.

In the logic scipt of LanguageUI,

public class LanguageUI : ScreenUI {
void OnEnable() {
var languageListView = Root.Q();
// Populate languageListView with language items…
// and then,
languageListView.itemsChosen += OnOK;
}
void OnOK(IEnumerable selectedItems) {
AdvanceTo(“EulaUI”);
}
}

When the user selects (Enter) an item in ListView, LanguageUI.OnOK() is invoked, LanguageUI is deactivated, and EulaUI is activated.
Then the error happens:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UIElements.BaseVerticalCollectionView.Apply

So I changed LanguageUI a bit,

public class LanguageUI : ScreenUI {
bool goNext;
void OnEnable() {
var languageListView = Root.Q();
// Populate languageListView with language items…
// and then,
languageListView.itemsChosen += OnOK;
goNext = false;
}
void OnOK(IEnumerable selectedItems) {
goNext = true;
}
void Update() {
if (goNext) {
AdvanceTo(m_NextScreen.gameObject.name);
}
}
}

Now the error NullReferenceException doesn’t come up.

My guess is that ListView can cleanup in Update but not in other execution phases.