Unity-Technologies/UnityCsReference · error · ArgumentException

Input arrays should have same length

Error message

Input arrays should have same length

What it means

Thrown by SetSortingColumns when columnIndices and sortAscending have different lengths. The two arrays are zipped element-by-element, so a length mismatch means some columns would lack a sort direction or vice versa.

Source

Thrown at Editor/Mono/GUI/TreeView/MultiColumnHeader.cs:78

            }
        }

        public MultiColumnHeader(MultiColumnHeaderState state)
        {
            m_State = state;
            m_HeaderButtonsControlID = GUIUtility.GetPermanentControlID();
        }

        public void SetSortingColumns(int[] columnIndices, bool[] sortAscending)
        {
            if (columnIndices == null)
                throw new ArgumentNullException("columnIndices");

            if (sortAscending == null)
                throw new ArgumentNullException("sortAscending");

            if (columnIndices.Length != sortAscending.Length)
                throw new ArgumentException("Input arrays should have same length");

            if (columnIndices.Length > state.maximumNumberOfSortedColumns)
                throw new ArgumentException("The maximum number of sorted columns is " + state.maximumNumberOfSortedColumns + ". Trying to set " + columnIndices.Length + " columns.");

            if (columnIndices.Length != columnIndices.DistinctCount())
                throw new ArgumentException("Duplicate column indices are not allowed", "columnIndices");

            bool changed = false;

            if (!columnIndices.AsSpan().SequenceEqual(state.sortedColumns))
            {
                state.sortedColumns = columnIndices;
                changed = true;
            }

            for (int i = 0; i < columnIndices.Length; ++i)
            {
                var column = GetColumn(columnIndices[i]);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Derive both arrays from the same source list of (columnIndex, ascending) pairs.
  2. Add an assertion columnIndices.Length == sortAscending.Length before calling.
  3. Truncate or pad the shorter array intentionally, with a logged warning, if mismatch is expected.

Example fix

// before
header.SetSortingColumns(cols, flags); // different lengths

// after
System.Diagnostics.Debug.Assert(cols.Length == flags.Length);
header.SetSortingColumns(cols, flags);
Defensive patterns

Strategy: validation

Validate before calling

if (columnIndices.Length != sortAscending.Length) throw new InvalidOperationException("length mismatch");

Prevention

When it happens

Trigger: Building the two arrays from different sources (e.g. columnIndices from saved state and sortAscending from a separate preference) that drifted out of sync.

Common situations: Partial deserialization where one array loaded fewer entries; UI state where the user changed selection count but the flags list wasn't updated.

Related errors


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