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
- Construct the TreeView with the MultiColumnHeader overload: new TreeView(state, multiColumnHeader).
- If you do not need columns, remove the columnIndexForTreeFoldouts assignment entirely.
- 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
- Use the TreeView(state, multiColumnHeader) constructor when you need columns.
- Guard columnIndexForTreeFoldouts assignments with a multiColumnHeader null check.
- Do not copy multi-column settings into single-column TreeViews.
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
- visibleColumns should should not be set to an empty array. A
- Invalid index for columnIndexForTreeFoldouts: {0}. Number of
- GetCellRect can only be called when 'multiColumnHeader' has
- Only call this method if you are using a MultiColumnHeader w
- TreeView: 'rootItem.children == null'. Did you forget to add
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/5d945a8327a99452.
Report an issue: GitHub.