Unity-Technologies/UnityCsReference · error · InvalidOperationException

GetCellRect can only be called when 'multiColumnHeader' has

Error message

GetCellRect can only be called when 'multiColumnHeader' has been set

What it means

Thrown by GetCellRectForTreeFoldouts when multiColumnHeader is null. The method delegates to multiColumnHeader.GetCellRect to compute the rectangle for the foldout within a specific column, so without a header there is no column geometry to query.

Source

Thrown at Editor/Mono/GUI/TreeView/TreeViewControl/TreeViewControl.cs:214

                    throw new InvalidOperationException("Setting columnIndexForTreeFoldouts can only be set when using TreeView with a MultiColumnHeader");

                if (value < 0 || value >= multiColumnHeader.state.columns.Length)
                    throw new ArgumentOutOfRangeException("value", string.Format("Invalid index for columnIndexForTreeFoldouts: {0}. Number of available columns: {1}", value, multiColumnHeader.state.columns.Length));

                m_GUI.columnIndexForTreeFoldouts = value;
            }
        }

        protected bool useScrollView
        {
            get { return m_TreeView.useScrollView; }
            set { m_TreeView.useScrollView = value; }
        }

        protected Rect GetCellRectForTreeFoldouts(Rect rowRect)
        {
            if (multiColumnHeader == null)
                throw new InvalidOperationException("GetCellRect can only be called when 'multiColumnHeader' has been set");

            int columnIndex = columnIndexForTreeFoldouts;
            int visibleColumnIndex = multiColumnHeader.GetVisibleColumnIndex(columnIndex);
            return multiColumnHeader.GetCellRect(visibleColumnIndex, rowRect);
        }

        protected float depthIndentWidth
        {
            get { return m_GUI.k_IndentWidth; }
            set { m_GUI.k_IndentWidth = value; }
        }

        protected bool showAlternatingRowBackgrounds { get; set; }
        protected bool showBorder { get; set; }
        protected bool showingHorizontalScrollBar { get { return m_TreeView.showingHorizontalScrollBar; }}
        protected bool showingVerticalScrollBar { get { return m_TreeView.showingVerticalScrollBar; } }
        internal bool sortByNameAfterSearch { get; set; } = true;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Only call GetCellRectForTreeFoldouts when multiColumnHeader != null.
  2. Branch row-drawing logic: use GetCellRectForTreeFoldouts for multi-column, use the full rowRect for single-column.
  3. Construct the TreeView with a MultiColumnHeader if you need column-based cell rects.

Example fix

// before
protected override void RowGUI(RowGUIArgs args)
{
    var foldoutRect = GetCellRectForTreeFoldouts(args.rowRect);
    // ...
}

// after
protected override void RowGUI(RowGUIArgs args)
{
    var foldoutRect = multiColumnHeader != null
        ? GetCellRectForTreeFoldouts(args.rowRect)
        : args.rowRect;
    // ...
}
Defensive patterns

Strategy: type-guard

Validate before calling

Rect foldoutRect = tree.multiColumnHeader != null
    ? tree.GetCellRectForTreeFoldouts(rowRect)
    : rowRect;

Type guard

static bool CanUseCellRect<T>(TreeView<T> tv) => tv.multiColumnHeader != null;

Prevention

When it happens

Trigger: Calling GetCellRectForTreeFoldouts in a row-drawing override on a TreeView that was constructed without a MultiColumnHeader; calling before the header is assigned.

Common situations: Overriding OnRowGUI (or similar) and unconditionally calling GetCellRectForTreeFoldouts; sharing draw code between single-column and multi-column TreeView subclasses.

Related errors


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