{"record":{"id":"38517f397b46fc94","repo":"Unity-Technologies/UnityCsReference","slug":"swapcolumn-is-only-supported-for-columns-next-to-e","errorCode":null,"errorMessage":"SwapColumn is only supported for columns next to each other otherwise visible columns and sorted columns will be incorrect. Column index {columnIndex}, targetColumnIndex {targetColumnIndex}","messagePattern":"SwapColumn is only supported for columns next to each other otherwise visible columns and sorted columns will be incorrect\\. Column index (.+?), targetColumnIndex (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GUI/TreeView/MultiColumnHeaderState.cs","lineNumber":115,"sourceCode":"                if (value != current)\n                {\n                    if (value >= 0)\n                    {\n                        m_SortedColumns.Remove(value);\n                        m_SortedColumns.Insert(0, value);\n                    }\n                    else\n                    {\n                        m_SortedColumns.Clear();\n                    }\n                }\n            }\n        }\n\n        internal void SwapColumns(int columnIndex, int targetColumnIndex)\n        {\n            if (Mathf.Abs(columnIndex - targetColumnIndex) != 1)\n                throw new ArgumentException(\"SwapColumn is only supported for columns next to each other otherwise visible columns and sorted columns will be incorrect. Column index \" + columnIndex + \", targetColumnIndex \" + targetColumnIndex);\n\n            // Swap columns\n            var temp = columns[columnIndex];\n            columns[columnIndex] = columns[targetColumnIndex];\n            columns[targetColumnIndex] = temp;\n\n            // Swap visible columns state so it matches swapped columns above\n            for (int i = 0; i < m_VisibleColumns.Length; i++)\n            {\n                if (m_VisibleColumns[i] == columnIndex)\n                    m_VisibleColumns[i] = targetColumnIndex;\n                else if (m_VisibleColumns[i] == targetColumnIndex)\n                    m_VisibleColumns[i] = columnIndex;\n            }\n            var list = new List<int>(m_VisibleColumns);\n            list.Sort();\n            m_VisibleColumns = list.ToArray();\n","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GUI/TreeView/MultiColumnHeaderState.cs#L97-L133","documentation":"Thrown by MultiColumnHeaderState.SwapColumns when columnIndex and targetColumnIndex are not adjacent (absolute difference != 1). The swap logic only updates visible/sorted column mappings correctly for neighboring columns, so non-adjacent swaps are forbidden to avoid corrupting state.","triggerScenarios":"Calling SwapColumns with indices differing by more than 1 (e.g. swapping column 0 with column 3).","commonSituations":"Drag-reorder UI allowing multi-step moves; a script attempting arbitrary column reordering via repeated SwapColumns.","solutions":["Implement arbitrary reorder as a sequence of single-step (adjacent) swaps between source and target.","Validate Mathf.Abs(columnIndex - targetColumnIndex) == 1 before calling.","If you need full reordering, rebuild the columns/visibleColumns arrays directly instead of using SwapColumns."],"exampleFix":"// before\nstate.SwapColumns(from, to); // |from-to| > 1 throws\n\n// after\nint step = from < to ? 1 : -1;\nfor (int i = from; i != to; i += step)\n    state.SwapColumns(i, i + step);","handlingStrategy":"validation","validationCode":"if (Mathf.Abs(columnIndex - targetColumnIndex) != 1) throw new InvalidOperationException(\"non-adjacent swap\");","typeGuard":"static bool AreAdjacent(int a, int b) => Mathf.Abs(a - b) == 1;","tryCatchPattern":null,"preventionTips":["Decompose arbitrary reorder into a chain of adjacent swaps.","Validate adjacency before calling SwapColumns.","For full reordering, rebuild the columns array directly."],"tags":["unity-editor","treeview","multicolumn","reorder"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}