Unity-Technologies/UnityCsReference · error · InvalidOperationException

Setting columnIndexForTreeFoldouts can only be set when usin

Error message

Setting columnIndexForTreeFoldouts can only be set when using TreeView with a MultiColumnHeader

What it means

Thrown by the setter of columnIndexForTreeFoldouts when multiColumnHeader is null. The foldout (expand/collapse arrow) column index is only meaningful when the TreeView uses a MultiColumnHeader; without one there are no columns to index into.

Source

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

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

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

        protected int columnIndexForTreeFoldouts
        {
            get { return m_GUI.columnIndexForTreeFoldouts; }
            set
            {
                if (multiColumnHeader == null)
                    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");

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Construct the TreeView with the MultiColumnHeader overload: new TreeView(state, multiColumnHeader).
  2. If you do not need columns, remove the columnIndexForTreeFoldouts assignment entirely.
  3. Ensure multiColumnHeader is assigned before setting columnIndexForTreeFoldouts.

Example fix

// before
var tree = new TreeView<int>(state);
tree.columnIndexForTreeFoldouts = 1; // throws

// after
var header = new MultiColumnHeader(headerState);
var tree = new TreeView<int>(state, header);
tree.columnIndexForTreeFoldouts = 1;
Defensive patterns

Strategy: type-guard

Validate before calling

if (tree.multiColumnHeader != null)
    tree.columnIndexForTreeFoldouts = desiredIndex;

Type guard

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

Prevention

When it happens

Trigger: Setting columnIndexForTreeFoldouts on a TreeView constructed without a MultiColumnHeader (i.e. the single-column TreeView(TreeViewState) constructor); setting the property before assigning a MultiColumnHeader.

Common situations: Copying code from a multi-column TreeView example into a single-column TreeView; refactoring that removes the MultiColumnHeader but leaves the foldout column setting behind.

Related errors


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