Unity-Technologies/UnityCsReference · error · InvalidOperationException

Invalid tree for finding descendants: Ensure a complete tree

Error message

Invalid tree for finding descendants: Ensure a complete tree when using this utillity method.

What it means

GetDescendantsThatHaveChildren (and relatives) walks the tree to collect parent IDs, but it requires a fully expanded materialized tree. If it encounters a child list that is the special lazy/collapsed sentinel (a single-item placeholder produced by LazyTreeViewDataSource for a collapsed parent), it cannot know the real descendants and throws InvalidOperationException. The error tells you to provide a complete tree, i.e. expand parents before calling.

Source

Thrown at Editor/Mono/GUI/TreeView/TreeViewUtililty.cs:114

        }

        // Assumes full tree
        internal static void GetParentsBelowItem(TreeViewItem<TIdentifier> fromItem, HashSet<TIdentifier> parentsBelow)
        {
            if (fromItem == null)
                throw new ArgumentNullException("fromItem");

            Stack<TreeViewItem<TIdentifier>> stack = new Stack<TreeViewItem<TIdentifier>>();
            stack.Push(fromItem);

            while (stack.Count > 0)
            {
                TreeViewItem<TIdentifier> current = stack.Pop();
                if (current.hasChildren)
                {
                    parentsBelow.Add(current.id);
                    if (LazyTreeViewDataSource<TIdentifier>.IsChildListForACollapsedParent(current.children))
                        throw new InvalidOperationException("Invalid tree for finding descendants: Ensure a complete tree when using this utillity method.");

                    foreach (var foo in current.children)
                    {
                        stack.Push(foo);
                    }
                }
            }
        }

        internal static void DebugPrintToEditorLogRecursive(TreeViewItem<TIdentifier> item)
        {
            if (item == null)
                return;
            System.Console.WriteLine(new System.String(' ', item.depth * 3) + item.displayName);

            if (!item.hasChildren)
                return;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure all relevant parents are expanded (SetExpanded on their ids) before invoking the descendant utility.
  2. Use a fully materialized tree built via SetChildParentReferences from complete data rather than the lazy source.
  3. If you only need visible rows, iterate GetRows() directly instead of walking the descendant tree.
  4. Detect lazy sentinel children (LazyTreeViewDataSource<T>.IsChildListForACollapsedParent) and expand those parents first.

Example fix

// before
var parents = new List<int>();
TreeViewUtility<int>.GetDescendantsThatHaveChildren(root, parents);

// after
foreach (var id in collapsedParentIds)
    treeViewState.expandedIDs.Add(id); // or SetExpanded
treeView.Reload();
var parents = new List<int>();
TreeViewUtility<int>.GetDescendantsThatHaveChildren(root, parents);
Defensive patterns

Strategy: validation

Validate before calling

// ensure parents expanded before walking descendants
foreach (var id in idsToExpand)
    state.expandedIDs.Add(id);
treeView.Reload();
// now safe to call GetDescendantsThatHaveChildren

Type guard

static bool IsCompleteTree<T>(TreeViewItem<T> item) where T : unmanaged, IEquatable<T>
{
    if (item == null) return false;
    if (item.hasChildren && LazyTreeViewDataSource<T>.IsChildListForACollapsedParent(item.children))
        return false;
    return true;
}

Prevention

When it happens

Trigger: Calling GetDescendantsThatHaveChildren on a tree built from a LazyTreeViewDataSource without expanding collapsed parents; running tree-walking utilities right after Reload before data source expansion; passing rows from GetRows() where collapsed parents still carry the lazy sentinel child.

Common situations: Custom tree view using lazy loading; a snapshot of the tree taken while some parents are collapsed; editor code that assumes all children are materialized.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/8bb8c01f793d87f6. Report an issue: GitHub.