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. Dismiss Notice

Question Why do Visual Elements don't have a method to remove themselves?

Discussion in 'UI Toolkit' started by niuage, Feb 13, 2023.

  1. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    88
    I'm mostly just curious why we have to pass through the parent to delete an element. It's entirely possible I'm missing something, but basically to remove an element, I usually do something like:

    Code (CSharp):
    1. VisualElement someElement = Doc.Q("selector");
    2. someElement.parent.Remove(someElement);
    Is there a better way? Why can't I just do
    Code (CSharp):
    1. someElement.Remove();
    Thanks.
     
  2. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    360
    You can use
    element.RemoveFromHierarchy();
    for this.

    Hope this helps!
     
    niuage and karl_jones like this.
  3. niuage

    niuage

    Joined:
    Nov 17, 2019
    Posts:
    88
    Idk how I missed that, it was right there in VS autocompletion. Thanks :)