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 Query can't find descendants

Discussion in 'UI Toolkit' started by fg_davevanegdom, Nov 2, 2022.

  1. fg_davevanegdom

    fg_davevanegdom

    Joined:
    Sep 9, 2021
    Posts:
    9
    Hello!

    I want to be able to find all ports on my node and ports with a matching direction to my other port to a list. However, it appears to me that the query is unable to find any ports that are somewhere in the visual tree on my node.

    Code (CSharp):
    1.  
    2.         public static int GetPortIndex<T>(this EditorDNode<T> node, Port port) where T : DNode
    3.         {
    4.             List<Port> matches = new List<Port>();
    5.             node.Query<Port>().ToList().ForEach(p => {
    6.                 if (p.direction == port.direction) matches.Add(p);
    7.             });
    8.             return matches.Count == 0 ? -1 : matches.IndexOf(port);
    9.         }
    10.  
    Debug.Log(node.Query<Port>().ToList().Count)
    prints 0.

    Any idea why this is not working?

    Kind regards,
    Dave
     
    Last edited: Nov 2, 2022
  2. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    Where are you calling this from? I found that querying too early sometimes leads to no matches. I guess the tree must initialize before we can properly query it.