Unity-Technologies/UnityCsReference · error · ArgumentNullException
root
Error message
root
What it means
Thrown by EditorToolbar.LoadToolbarElements when the root VisualElement argument is null. The method must attach style sheets and append toolbar elements to this root, so a null root would cause a NullReferenceException deeper in UI Toolkit without this early guard.
Source
Thrown at Editor/Mono/GUI/Toolbars/EditorToolbar.cs:59
public static OverlayToolbar CreateOverlay(IEnumerable<string> toolbarElementIds, EditorWindow context = null)
{
var root = new OverlayToolbar();
foreach (var id in toolbarElementIds)
{
if (TryCreateElement(id, context, out var ve))
root.Add(ve);
}
return root;
}
// Used by MainToolbar, as it doesn't use the same Overlay styling
internal void LoadToolbarElements(VisualElement root)
{
if (root == null)
throw new ArgumentNullException(nameof(root));
EditorToolbarUtility.LoadStyleSheets("EditorToolbar", root);
foreach (var id in m_ToolbarElements)
{
if(TryCreateElement(id, m_Context, out var ve))
root.Add(ve);
}
}
static bool TryCreateElement(string id, EditorWindow ctx, out VisualElement ve)
{
if (EditorToolbarManager.instance.TryCreateElementFromId(ctx, id, out ve))
{
if (ve is IAccessContainerWindow visualWithContext)
visualWithContext.containerWindow = ctx;
ve.AddToClassList(elementClassName);
return true;View on GitHub (pinned to 225b0fbdb5)
Solutions
- Ensure the EditorWindow's rootVisualElement is created before calling LoadToolbarElements.
- Pass a valid container VisualElement created with 'new VisualElement()' if a detached root is intended.
- Defer the call until the window's CreateGUI / OnEnable has run.
Example fix
// before
LoadToolbarElements(null);
// after
var root = new VisualElement { name = "toolbar-root" };
LoadToolbarElements(root); Defensive patterns
Strategy: validation
Validate before calling
if (root == null) throw new ArgumentNullException(nameof(root)); // or create one: root ??= new VisualElement();
Type guard
static bool IsReady(VisualElement root) => root != null;
Prevention
- Call LoadToolbarElements only after the EditorWindow rootVisualElement is created.
- Pass a freshly created VisualElement for detached roots.
- Initialize UI in OnEnable/CreateGUI, not in the constructor.
When it happens
Trigger: Calling LoadToolbarElements passing a null VisualElement; the owning EditorWindow's rootVisualElement not yet initialized at call time.
Common situations: Invoking toolbar load before the window's UI tree exists; a refactor that changed the caller to pass a not-yet-created element.
Related errors
- Array is invalid.
- The source path cannot be empty.
- Path cannot be null or empty.
- Specify exactly one edge
- ApplyPrefabAddedGameObjects requires that GameObjects share
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/b8f85e81967c8c84.
Report an issue: GitHub.