UI Toolkit works in Editor but gives errors in WebGL build

Hi there. We are using UI Toolkit in our game and want to build to WebGL. Previously this has all worked perfectly without issue. However all of the sudden I am getting this error in the Javascript console and it seems that UI Document don’t work / rootVisualElement is not set. However no other errors are logged so I am unsure how to proceed.

Web.framework.js:10 NullReferenceException: Object reference not set to an instance of an object.
  at UnityEngine.UIElements.StyleSheets.HierarchyTraversal.Recurse (UnityEngine.UIElements.VisualElement element, System.Int32 depth) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.UIElements.UQuery+UQueryMatcher.TraverseRecursive (UnityEngine.UIElements.VisualElement element, System.Int32 depth) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.UIElements.StyleSheets.HierarchyTraversal.Traverse (UnityEngine.UIElements.VisualElement element) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.UIElements.UQuery+UQueryMatcher.Traverse (UnityEngine.UIElements.VisualElement element) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.UIElements.UQuery+UQueryMatcher.Run (UnityEngine.UIElements.VisualElement root, System.Collections.Generic.List`1[T] matchers) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.UIElements.UQuery+SingleQueryMatcher.Run (UnityEngine.UIElements.VisualElement root, System.Collections.Generic.List`1[T] matchers) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.UIElements.UQueryState`1[T].Single (UnityEngine.UIElements.UQuery+SingleQueryMatcher matcher) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.UIElements.UQueryState`1[T].First () [0x00000] in <00000000000000000000000000000000>:0

What I have already tried:

  • Rolling back did nott seem to change anything
  • Enabling / diisabling all ui documents (Stops the error but kinda need my ui ;))
  • Clear library folder

I really hope someone has a possible solution to try! Thanks in advance.

It looks like it might have to do with StyleSheets perhaps?

One of the main differences with a WebGL build is extra access control build into the browser about retrieving remote resource files.

Doing so requires consideration of CORS (Cross-Origin Resource Sharing) otherwise the resource won’t load.

This might provide you some avenue of investigation if perhaps the elements you are using access some remote CSS file.