Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug Bug in UI Builder (NullRef) when building custom element tree from uxml (Unity 2021.2.6f1)

Discussion in 'UI Toolkit' started by pyriceti, Dec 27, 2021.

  1. pyriceti

    pyriceti

    Joined:
    Sep 29, 2015
    Posts:
    8
    Hi,

    I came across a bug investigating UI Toolkit features. I use the UI Builder to compose my editor visual elements, and I want to avoid to build visual trees in C# to maximize UXML re-usage. For a visual "module" of an Editor tool, the idea is to create a custom Visual Element in C# to code any specific behaviour, to load a UXML tree to setup its layout, and voilà.

    So, for instance, let's have a custom VisualElement named TestElement, that have a Label as a child element. Since I want to avoid building the visual tree in C#, my class contains the following:

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine.UIElements;
    4.  
    5. public class TestElement : VisualElement
    6. {
    7.     public new class UxmlFactory : UxmlFactory<TestElement, UxmlTraits> { }
    8.  
    9.     public TestElement()
    10.     {
    11.         VisualTreeAsset uxml = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/UXML/TestElement.uxml");
    12.         TemplateContainer root = uxml!.Instantiate();
    13.         this.hierarchy.Add(root);
    14.     }
    15. }
    16.  
    When I select the Label element in the Hierarchy Panel of the UI Builder, I get a NullReferenceException and the Inspector Panel doesn't refresh all the Label infos correctly.
    upload_2021-12-27_16-42-10.png

    The stack trace is:

    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)
    ...


    I created the its counterpart that creates the Label in C#, called TestElementWithTree:

    Code (CSharp):
    1.  
    2. using UnityEngine.UIElements;
    3.  
    4. public class TestElementWithTree : VisualElement
    5. {
    6.     public new class UxmlFactory : UxmlFactory<TestElementWithTree, UxmlTraits> { }
    7.  
    8.     public TestElementWithTree()
    9.     {
    10.         this.hierarchy.Add(new Label());
    11.     }
    12. }
    13.  
    And everything is fine in this case:
    upload_2021-12-27_16-44-11.png

    I'm using Unity 2021.2.6f1. Is it a known bug? Am I doing something wrong? Should I report the bug?

    Thanks!

    Update 1:

    Reproductible in 2021.2.7f1 as @fipsy noted.
    Also occurs if we use CloneTree method:

    Code (CSharp):
    1.  
    2.     public TestElement()
    3.     {
    4.         VisualTreeAsset uxml = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/UXML/TestElement.uxml");
    5.         uxml.CloneTree(this);
    6.     }
    7.  
     
    Last edited: Dec 31, 2021
  2. fipsy

    fipsy

    Joined:
    Sep 3, 2016
    Posts:
    13
    I have exactly the same problem. Help would be greatly appreciated!

    Edit: I'm using 2021.2.7f1
     
    pyriceti likes this.
  3. Pixii

    Pixii

    Joined:
    Nov 7, 2019
    Posts:
    18
    Has there been an update on this? I am also having this issue in 2021.2.17f
     
    Walter_Hulsebos likes this.
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    735
    Hi,
    Please report a bug, I don't believe that is known.
     
  5. Cookie042

    Cookie042

    Joined:
    Nov 25, 2013
    Posts:
    9
    Done.

    ^ i dont know if someone else ended up reporting it... @ other people in thread.
     
  6. roman_sedition

    roman_sedition

    Joined:
    Nov 6, 2016
    Posts:
    13
    this is still a thing in 2021.3
     
  7. CodeWithKyrian

    CodeWithKyrian

    Joined:
    Aug 17, 2017
    Posts:
    9
    Any update on this?? I still have this problem in 2021.3.6f1 and I really don't want to be contructing my whole custom custrol in C#
     
  8. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    735
    Would you be able to post the case number here?
     
  9. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    Just tried ui toolkit after one year and this bug is still here in lts 2021.3.12....

    And because there's no source access anymore I can't fix it myself. Back to UGUI.
     
    Last edited: Oct 27, 2022
    Snoozed likes this.
  10. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    199
    This is happening in 2022.2.1f1

    Code (StackTrace):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Unity.UI.Builder.BuilderInspectorAttributes.RefreshAttributeField (UnityEngine.UIElements.BindableElement fieldElement) (at <81f375d10d894628b5e16e29eeddc039>:0)
    3. Unity.UI.Builder.BuilderInspectorAttributes.CreateAttributeRow (UnityEngine.UIElements.VisualElement parent, UnityEngine.UIElements.UxmlAttributeDescription attribute) (at <81f375d10d894628b5e16e29eeddc039>:0)
    4. Unity.UI.Builder.BuilderInspectorAttributes.GenerateAttributeFields () (at <81f375d10d894628b5e16e29eeddc039>:0)
    5. Unity.UI.Builder.BuilderInspectorAttributes.Refresh () (at <81f375d10d894628b5e16e29eeddc039>:0)
    6. Unity.UI.Builder.BuilderInspector.RefreshUI () (at <81f375d10d894628b5e16e29eeddc039>:0)
    7. Unity.UI.Builder.BuilderInspector.SelectionChanged () (at <81f375d10d894628b5e16e29eeddc039>:0)
    8. Unity.UI.Builder.BuilderSelection.NotifyOfSelectionChange (Unity.UI.Builder.IBuilderSelectionNotifier source) (at <81f375d10d894628b5e16e29eeddc039>:0)
    9. Unity.UI.Builder.BuilderSelection.AddToSelection (Unity.UI.Builder.IBuilderSelectionNotifier source, UnityEngine.UIElements.VisualElement ve, System.Boolean undo, System.Boolean sort) (at <81f375d10d894628b5e16e29eeddc039>:0)
    10. Unity.UI.Builder.BuilderSelection.AddToSelection (Unity.UI.Builder.IBuilderSelectionNotifier source, UnityEngine.UIElements.VisualElement ve) (at <81f375d10d894628b5e16e29eeddc039>:0)
    11. Unity.UI.Builder.BuilderExplorer.ElementSelectionChanged (System.Collections.Generic.List`1[T] elements) (at <81f375d10d894628b5e16e29eeddc039>:0)
    12. Unity.UI.Builder.ElementHierarchyView.OnSelectionChange (System.Collections.Generic.IEnumerable`1[T] itemIndices) (at <81f375d10d894628b5e16e29eeddc039>:0)
    13. UnityEngine.UIElements.BaseVerticalCollectionView.NotifyOfSelectionChange () (at <469e18a818964f39910fa06bef052d82>:0)
    14. UnityEngine.UIElements.BaseVerticalCollectionView.SetSelectionInternal (System.Collections.Generic.IEnumerable`1[T] indices, System.Boolean sendNotification) (at <469e18a818964f39910fa06bef052d82>:0)
    15. UnityEngine.UIElements.BaseVerticalCollectionView.SetSelection (System.Collections.Generic.IEnumerable`1[T] indices) (at <469e18a818964f39910fa06bef052d82>:0)
    16. UnityEngine.UIElements.BaseVerticalCollectionView.SetSelection (System.Int32 index) (at <469e18a818964f39910fa06bef052d82>:0)
    17. UnityEngine.UIElements.BaseVerticalCollectionView.DoSelect (UnityEngine.Vector2 localPosition, System.Int32 clickCount, System.Boolean actionKey, System.Boolean shiftKey) (at <469e18a818964f39910fa06bef052d82>:0)
    18. UnityEngine.UIElements.BaseVerticalCollectionView.ProcessPointerDown (UnityEngine.UIElements.IPointerEvent evt) (at <469e18a818964f39910fa06bef052d82>:0)
    19. UnityEngine.UIElements.BaseVerticalCollectionView.OnPointerDown (UnityEngine.UIElements.PointerDownEvent evt) (at <469e18a818964f39910fa06bef052d82>:0)
    20. UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <469e18a818964f39910fa06bef052d82>:0)
    21. UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <469e18a818964f39910fa06bef052d82>:0)
    22. UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <469e18a818964f39910fa06bef052d82>:0)
    23. UnityEngine.UIElements.CallbackEventHandler.HandleEventAtCurrentTargetAndPhase (UnityEngine.UIElements.EventBase evt) (at <469e18a818964f39910fa06bef052d82>:0)
    24. UnityEngine.UIElements.CallbackEventHandler.UnityEngine.UIElements.IEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <469e18a818964f39910fa06bef052d82>:0)
    25. UnityEngine.UIElements.EventDispatchUtilities.HandleEventAcrossPropagationPath (UnityEngine.UIElements.EventBase evt) (at <469e18a818964f39910fa06bef052d82>:0)
    26. UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at <469e18a818964f39910fa06bef052d82>:0)
    27. UnityEngine.UIElements.PointerEventDispatchingStrategy.SendEventToTarget (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <469e18a818964f39910fa06bef052d82>:0)
    28. UnityEngine.UIElements.PointerEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <469e18a818964f39910fa06bef052d82>:0)
    29. UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at <469e18a818964f39910fa06bef052d82>:0)
    30. UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <469e18a818964f39910fa06bef052d82>:0)
    31. UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at <469e18a818964f39910fa06bef052d82>:0)
    32. UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at <469e18a818964f39910fa06bef052d82>:0)
    33. UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <469e18a818964f39910fa06bef052d82>:0)
    34. UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& eventHandled) (at <469e18a818964f39910fa06bef052d82>:0)
    35. UnityEngine.UIElements.UIEventRegistration.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <469e18a818964f39910fa06bef052d82>:0)
    36. UnityEngine.UIElements.UIEventRegistration+<>c.<.cctor>b__1_2 (System.Int32 i, System.IntPtr ptr) (at <469e18a818964f39910fa06bef052d82>:0)
    37. UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& result) (at <3859f4a893614f508581f22c26c521c2>:0)
     
    Walter_Hulsebos likes this.
  11. Auroch-ChrisW

    Auroch-ChrisW

    Joined:
    May 4, 2022
    Posts:
    7
    Are you able to provide any updates/info about this issue? It's causing a major issue for our intended UITK workflow at present.
     
  12. Auroch-ChrisW

    Auroch-ChrisW

    Joined:
    May 4, 2022
    Posts:
    7
    Looking into this further, it appears that this happens because this method in UnityEngine.UIElements.VisualTreeAsset:

    Code (CSharp):
    1. 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 by our code. The only instance that this doesn't happen is in UnityEngine.UIElements.TemplateContainer.UxmlTraits.Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) - which is where embedded TemplateContainer instances seem to initialise their content when they want to do exactly this.

    Unfortunately because both the CloneTree method above and TemplateAsset.AttributeOverride are internal, we can't use them to get this working. If the publicly available Clone() or Instantiate() methods passed in an empty collection instead, or if both CloneTree and AttributeOverride were no longer internal, then the issue could be resolved.

    For the moment we're going to have to use a more convoluted UITK workflow to allow our programmers to add functionality to templates developed by our UI team. Absolutely not ideal though.

    Note - tested in both 2022.1.15 and 2021.3.21
     
  13. pierre_10

    pierre_10

    Unity Technologies

    Joined:
    Apr 1, 2016
    Posts:
    33
  14. Auroch-ChrisW

    Auroch-ChrisW

    Joined:
    May 4, 2022
    Posts:
    7
    Fantastic news, thank you :)
     
  15. Auroch-ChrisW

    Auroch-ChrisW

    Joined:
    May 4, 2022
    Posts:
    7
    Heya, thanks for creating the issue. I'm unable to log in to the issue tracker to vote/comment (issue tracker automatically logs me out as soon as I log in) so thought I'd ask here - is it likely that this will be fixed in 2021.3.x or 2022.x? It would be super useful for an active project, but we're currently using 2021 until 2022 has an LTS release - and I suspect we won't be using 2023 at all.

    It would be a massive shame to have to wait over a year before this fix is available in an LTS version.
     
  16. Tauntastic

    Tauntastic

    Joined:
    Sep 24, 2020
    Posts:
    15
    Is there any update on this issue?
     
    Last edited: Jun 26, 2023
    Auroch-ChrisW likes this.
  17. Auroch-ChrisW

    Auroch-ChrisW

    Joined:
    May 4, 2022
    Posts:
    7
    Quick update - after following this up with Unity Support, a fix is being reportedly being backported to the 2022 LTS. The issue raised by pierre_10 has been updated to reflect this.