Search Unity

Bug Exception when RemoveFromHierarchy

Discussion in 'UI Toolkit' started by StefanWo, Apr 5, 2023.

  1. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    123
    I just updated from Unity 2021.3.19f to Unity 2022.2.13.f1 and got the following error when trying to remove an element:

    It just happen just there in my popup manager, the rest of the game seems fine. No idea how to debug the problem there.

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.UIElements.VisualElement.WillChangePanel (UnityEngine.UIElements.BaseVisualElementPanel destinationPanel) (at <baee2ba96d3743838f9756565b830367>:0)
    3. UnityEngine.UIElements.VisualElement.SetPanel (UnityEngine.UIElements.BaseVisualElementPanel p) (at <baee2ba96d3743838f9756565b830367>:0)
    4. UnityEngine.UIElements.VisualElement+Hierarchy.SetParent (UnityEngine.UIElements.VisualElement value) (at <baee2ba96d3743838f9756565b830367>:0)
    5. UnityEngine.UIElements.VisualElement+Hierarchy.RemoveAt (System.Int32 index) (at <baee2ba96d3743838f9756565b830367>:0)
    6. UnityEngine.UIElements.VisualElement+Hierarchy.Remove (UnityEngine.UIElements.VisualElement child) (at <baee2ba96d3743838f9756565b830367>:0)
    7. UnityEngine.UIElements.VisualElement.RemoveFromHierarchy () (at <baee2ba96d3743838f9756565b830367>:0)
    8. PopupManager.ClosePopup (System.Boolean easy) (at Assets/Assemblies/Core/UI/PopupManager.cs:177)
    Update: Also found it when stopping playmode in unity, without my code involved:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.UIElements.VisualElement.WillChangePanel (UnityEngine.UIElements.BaseVisualElementPanel destinationPanel) (at <baee2ba96d3743838f9756565b830367>:0)
    3. UnityEngine.UIElements.VisualElement.SetPanel (UnityEngine.UIElements.BaseVisualElementPanel p) (at <baee2ba96d3743838f9756565b830367>:0)
    4. UnityEngine.UIElements.VisualElement+Hierarchy.SetParent (UnityEngine.UIElements.VisualElement value) (at <baee2ba96d3743838f9756565b830367>:0)
    5. UnityEngine.UIElements.VisualElement+Hierarchy.RemoveAt (System.Int32 index) (at <baee2ba96d3743838f9756565b830367>:0)
    6. UnityEngine.UIElements.VisualElement+Hierarchy.Remove (UnityEngine.UIElements.VisualElement child) (at <baee2ba96d3743838f9756565b830367>:0)
    7. UnityEngine.UIElements.VisualElement.RemoveFromHierarchy () (at <baee2ba96d3743838f9756565b830367>:0)
    8. UnityEngine.UIElements.UIDocumentList.RemoveFromListAndFromVisualTree (UnityEngine.UIElements.UIDocument uiDocument) (at <baee2ba96d3743838f9756565b830367>:0)
    9. UnityEngine.UIElements.PanelSettings.DetachUIDocument (UnityEngine.UIElements.UIDocument uiDocument) (at <baee2ba96d3743838f9756565b830367>:0)
    10. UnityEngine.UIElements.UIDocument.RemoveFromHierarchy () (at <baee2ba96d3743838f9756565b830367>:0)
    11. UnityEngine.UIElements.UIDocument.OnDisable () (at <baee2ba96d3743838f9756565b830367>:0)
    Also when i just close the scene. Happens all the time... And afterwards getting follow up errors, i guess due to the stop of the remove. Its really a blocking issue :(
     
    Last edited: Apr 7, 2023
  2. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    123
    I investigated more in it and it looks like the panel got null in the willChange method. I use a lot of times the RemoveFromHirarchy method in DetachFromPanel Events with different usecases. I can't debug in that part of Unity, so my only solution is to workaround each single part...

    I was able to make one of the usecases simple. This code causing the exception:
    Code (CSharp):
    1. var inside = new VisualElement();
    2. var middle = new VisualElement();
    3. var outside = new VisualElement();
    4. middle.Add(inside);
    5. outside.Add(middle);
    6. inside.RegisterCallback<DetachFromPanelEvent>(evt => middle.RemoveFromHierarchy());
    7. middle.RegisterCallback<DetachFromPanelEvent>(evt => outside.RemoveFromHierarchy());
    8. rootVisualElement.Add(outside);
    9. outside.RemoveFromHierarchy();
     
  3. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    123
    Anyone? I prepared already a testcase for it :)
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    It's awesome that you have this test case ready. Can you submit it through Unity ? Help > Report a bug.

    It's the most reliable way to get it to our developers.
     
  5. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    123
    I got this error now also in the compiled version from players, it seems my workarounds didn't cover all cases of it.

    After the first Bug report was rejected and i got no clear response what they did, i invested more time and created an empty project with just that testcase in it and submitted another one. Lets see if this one is accepted...
     
  6. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    123
    Finally an answer, after 2 tries of get an accepted bug-report:

    Already known but internal ticket: Closed... Like all my other reports, lots of time to debug, strip the error down in a simple case to get through and than just closed without further notice... Wasted effort to help unity to get better, never again wasting more time in that...
     
  7. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Can share your ticket number please ? I'll see if I can provide clarity on what happened.
     
  8. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    123
    IN-38880
     
  9. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    The ticket was closed by accident. It has been re-opened and it should get to the dev team soon. Sorry.
     
    StefanWo likes this.
  10. StefanWo

    StefanWo

    Joined:
    May 24, 2015
    Posts:
    123