Search Issue Tracker

Fixed

Fixed in 2021.3.31f1, 2022.3.10f1, 2023.1.13f1, 2023.2.0b9, 2023.3.0a4

Votes

2

Found in

2021.3.22f1

2021.3.29f1

2022.1.24f1

2022.2.13f1

2023.1.0b10

2023.2.0b4

2023.3.0a1

Issue ID

UUM-32501

Regression

Yes

[UI Builder] NullReferenceException when building custom element tree from uxml

--

-

Steps to repro:

1. Create a custom *VisualElement* named {*}TestElement{*}, that has a *Label* as a child element. Since I want to avoid building the visual tree in C#, my class contains the following:
  #
using UnityEditor;
#
using UnityEngine.UIElements;
#
 
#
public class TestElement : VisualElement
#
{
#
    public [new|http://www.google.com/search?q=new+msdn.microsoft.com] class UxmlFactory : UxmlFactory<TestElement, UxmlTraits> \{ }
#
 
#
    public TestElement()
#
    {
#
        VisualTreeAsset uxml = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/UXML/TestElement.uxml");
#
        TemplateContainer root = uxml!.Instantiate();
#
        this.hierarchy.Add(root);
#
    }
#
}

2. When selecting the Label element in the Hierarchy Panel of the UI Builder, you get a NullReferenceException and the Inspector Panel doesn't refresh all the Label infos correctly.

NullReferenceException: Object reference not set to an instance of an object
Unity.UI.Builder.BuilderInspectorAttributes.RefreshAttributeField (UnityEngine.UIElements.BindableElement fieldElement) (at <415ec9d438ed4f3badc4f75a27f3e7ff>:0)
Unity.UI.Builder.BuilderInspectorAttributes.CreateAttributeRow (UnityEngine.UIElements.UxmlAttributeDescription attribute) (at <415ec9d438ed4f3badc4f75a27f3e7ff>:0)
Unity.UI.Builder.BuilderInspectorAttributes.GenerateAttributeFields () (at <415ec9d438ed4f3badc4f75a27f3e7ff>:0)
Unity.UI.Builder.BuilderInspectorAttributes.Refresh () (at <415ec9d438ed4f3badc4f75a27f3e7ff>:0)
Unity.UI.Builder.BuilderInspector.RefreshUI () (at <415ec9d438ed4f3badc4f75a27f3e7ff>:0)
Unity.UI.Builder.BuilderInspector.SelectionChanged () (at <415ec9d438ed4f3badc4f75a27f3e7ff>:0)

 

The culprit seems to be:
 
internal void CloneTree(VisualElement target, Dictionary<string, VisualElement> slotInsertionPoints, List<TemplateAsset.AttributeOverride> attributeOverrides)
 
... gets called with the attributeOverrides set to null whenever Clone() or Instantiate() is called

  1. Resolution Note (fix version 2023.3.0a4):

    Fixed UUM-32501: UI Toolkit: Fixed selection in custom controls in UI Builder

  2. Resolution Note (fix version 2023.2.0b9):

    Fixed UUM-32501: UI Toolkit: Fixed selection in custom controls in UI Builder

  3. Resolution Note (fix version 2023.1.13f1):

    Fixed UUM-32501: UI Toolkit: Fixed selection in custom controls in UI Builder

  4. Resolution Note (fix version 2022.3.10f1):

    Fixed UUM-32501: UI Toolkit: Fixed selection in custom controls in UI Builder

  5. Resolution Note (fix version 2021.3.31f1):

    Fixed UUM-32501: UI Toolkit: Fixed selection in custom controls in UI Builder

Comments (3)

  1. iletai

    Jul 20, 2024 03:02

    That issues not yet fix in Unity 2022.3.f1

  2. AurochElysia

    Jun 26, 2023 11:52

    Hello! This would be really useful to have fixed in 2022.x as well :)

  3. Gwendal_Broudin_Emoko

    May 12, 2023 12:14

    This bug prevent us to use UI toolkit on Unity 2021.3 could it be back ported easily or could you provide a workaround ?

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.