Unity-Technologies/UnityCsReference · error · ArgumentException

Invalid argument: {dropPosition}

Error message

Invalid argument: {dropPosition}

What it means

Thrown by GetPreviousAndNextItemsIgnoringDraggedItems when the supplied DropPosition is anything other than Above or Below (i.e. Upon or an out-of-range value). The method computes neighboring rows for sibling insertion, an operation that is meaningless for a 'drop onto' position, so it rejects the input with an ArgumentException. The message concatenates the offending enum value so you can see exactly what was passed.

Source

Thrown at Editor/Mono/GUI/TreeView/TreeViewDragging.cs:126

        // When hovering outside any items: target and parent is null, dropPos is invalid
        // If parentItem and targetItem is the same then insert as first child of parent, dropPos is invalid
        // If parentItem and targetItem is different then use dropPos to insert dragged items relative to targetItem
        // parentItem can be null when root is visible and hovering above or below the root

        // if targetItem is null then parent can be null if root is visible
        // if targetitem is null then parent might be valid if root is hidden
        public virtual DragAndDropVisualMode DoDrag(TreeViewItem<TIdentifier> parentItem, TreeViewItem<TIdentifier> targetItem, bool perform, DropPosition dropPosition) => DoDragInternal(parentItem, targetItem, perform, dropPosition);
        public virtual DragAndDropVisualMode DoDragInternal(TreeViewItem<TIdentifier> parentItem, TreeViewItem<TIdentifier> targetItem, bool perform, DropPosition dropPosition) => throw new NotImplementedException();

        protected float GetDropBetweenHalfHeight(TreeViewItem<TIdentifier> item, Rect itemRect)
        {
            return m_TreeView.data.CanBeParent(item) ? m_TreeView.gui.halfDropBetweenHeight : itemRect.height * 0.5f;
        }

        void GetPreviousAndNextItemsIgnoringDraggedItems(int targetRow, DropPosition dropPosition, out TreeViewItem<TIdentifier> previousItem, out TreeViewItem<TIdentifier> nextItem)
        {
            if (dropPosition != DropPosition.Above && dropPosition != DropPosition.Below)
                throw new ArgumentException("Invalid argument: " + dropPosition);

            previousItem = nextItem = null;
            int curPrevRow = (dropPosition == DropPosition.Above) ? targetRow - 1 : targetRow;
            int curNextRow = (dropPosition == DropPosition.Above) ? targetRow : targetRow + 1;

            while (curPrevRow >= 0)
            {
                var curPreviousItem = m_TreeView.data.GetItem(curPrevRow);
                if (!m_TreeView.IsDraggingItem(curPreviousItem))
                {
                    previousItem = curPreviousItem;
                    break;
                }
                curPrevRow--;
            }

            while (curNextRow < m_TreeView.data.rowCount)
            {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the DropPosition passed into sibling-insertion code is strictly DropPosition.Above or DropPosition.Below before calling.
  2. If your code can legitimately receive DropPosition.Upon, branch early and only call this method in the Above/Below branch.
  3. When overriding DoDrag/DoDragInternal, map Upon to Above or Below (or skip the call) instead of forwarding it.
  4. Validate the enum at the boundary of your own method and reject/map unexpected values before they reach TreeView internals.

Example fix

// before
GetPreviousAndNextItemsIgnoringDraggedItems(row, dropPosition, out prev, out next);

// after
if (dropPosition != DropPosition.Above && dropPosition != DropPosition.Below)
    return; // 'Upon' is handled by drop-onto-parent logic elsewhere
GetPreviousAndNextItemsIgnoringDraggedItems(row, dropPosition, out prev, out next);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidSiblingDropPosition(TreeViewDragging<int>.DropPosition p)
    => p == TreeViewDragging<int>.DropPosition.Above || p == TreeViewDragging<int>.DropPosition.Below;

Prevention

When it happens

Trigger: Calling GetPreviousAndNextItemsIgnoringDraggedItems directly with DropPosition.Upon; passing a DropPosition value that the internal drag pipeline only expects to be Above/Below; a custom TreeViewDragging<T> subclass that overrides drag handling and forwards a non-Above/Below DropPosition into sibling-insertion logic; an uninit/default DropPosition (== Upon, value 0) leaking into the path.

Common situations: Implementing a custom editor TreeView drag-drop handler and forgetting that 'Upon' is only valid for drop-onto-parent semantics; upgrading Unity where DropPosition enum gained/changed members; copy-pasting a drop handler that works for Upon drops but reusing it for between-row insertion.

Related errors


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