Search Unity

Resolved (figured it out) VisualElement code runs even though its parent GameObject is disabled

Discussion in 'UI Toolkit' started by LukasXenophanes, Jul 11, 2021.

  1. LukasXenophanes

    LukasXenophanes

    Joined:
    Apr 14, 2021
    Posts:
    7
    I created two GameObjects, each containing another UI document.
    I disabled one of the GameObjects, UIPickCardColumn, so i can see only the enabled one.
    upload_2021-7-11_17-17-14.png
    It looks like the code in UIPickCardColumn still runs though (which i don't agree with but who am i to say that), and also has its VisualElements set to null.
    UIPickCardColumn contains a single custom VisualElement at the root, PickCardColumn.
    Its code is:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UIElements;
    3.  
    4. public class PickCardColumn : VisualElement
    5. {
    6.     public new class UxmlFactory : UxmlFactory<PickCardColumn, UxmlTraits> { }
    7.     public PickCardColumn()
    8.     {
    9.         this.RegisterCallback<GeometryChangedEvent>(OnGeometryChange);
    10.     }
    11.     void OnGeometryChange(GeometryChangedEvent evt)
    12.     {
    13.         // test query
    14.         VisualElement ve = this.Query($"ColumnPicker0");
    15.         Debug.Log(ve);
    16.         ve.style.display = DisplayStyle.Flex;
    17.         this.UnregisterCallback<GeometryChangedEvent>(OnGeometryChange);
    18.     }
    19. }
    Console output:
    upload_2021-7-11_17-19-14.png

    Note how the VisualElement "ColumnPicker0" is set to null.

    When i enable the other UI Document, everything works fine, and the VisualElement is not null, so i assume the issue is not a typo in the code.
    upload_2021-7-11_17-24-22.png
    Console output:
    upload_2021-7-11_17-21-13.png
     
  2. LukasXenophanes

    LukasXenophanes

    Joined:
    Apr 14, 2021
    Posts:
    7
    The issue was that in UIBuilder, the wrong custom VisualElement was placed in the UI document UIPhasePlayCards. It contained the same VisualElement as UIPickCardColumn. I couldn't tell it was the wrong one because i renamed it to the name of the correct one in UIBuilder. I couldn't find way in UI Builder to find out which .cs file a visual element is defined in, besides its name, which can be changed.