Unity-Technologies/UnityCsReference · error · ArgumentOutOfRangeException

Must be 0, 1, or 2

Error message

Must be 0, 1, or 2

What it means

IsAxisEnabled(int vector3Axis) maps integer indices 0 to X, 1 to Y, 2 to Z within the Axes enum flags. Any value outside 0-2 is invalid because Vector3 and the Axes enum only define three axes.

Source

Thrown at Editor/Mono/Handles/BoundsHandle/PrimitiveBoundsHandle.cs:234

        }

        protected bool IsAxisEnabled(Axes axis)
        {
            return (axes & axis) == axis;
        }

        protected bool IsAxisEnabled(int vector3Axis)
        {
            switch (vector3Axis)
            {
                case 0:
                    return IsAxisEnabled(Axes.X);
                case 1:
                    return IsAxisEnabled(Axes.Y);
                case 2:
                    return IsAxisEnabled(Axes.Z);
                default:
                    throw new ArgumentOutOfRangeException("vector3Axis", "Must be 0, 1, or 2");
            }
        }

        private void MidpointHandles(ref Vector3 minPos, ref Vector3 maxPos, bool isCameraInsideBox)
        {
            Vector3 xAxis = Vector3.right;
            Vector3 yAxis = Vector3.up;
            Vector3 zAxis = Vector3.forward;
            Vector3 middle = (maxPos + minPos) * 0.5f;

            Vector3 localPos, newPos;
            if (IsAxisEnabled(Axes.X))
            {
                // +X
                localPos = new Vector3(maxPos.x, middle.y, middle.z);
                newPos = MidpointHandle(m_ControlIDs[(int)HandleDirection.PositiveX], localPos, yAxis, zAxis, isCameraInsideBox);
                maxPos.x = Mathf.Max(newPos.x, minPos.x);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the axis index is within 0-2 before calling; validate or clamp.
  2. Prefer calling IsAxisEnabled(Axes) with the enum value directly instead of the integer overload.

Example fix

// before
for (int i = 0; i <= 3; i++)
    if (IsAxisEnabled(i)) { ... }
// after
foreach (Axes axis in new[] { Axes.X, Axes.Y, Axes.Z })
    if (IsAxisEnabled(axis)) { ... }
Defensive patterns

Strategy: validation

Validate before calling

if (vector3Axis < 0 || vector3Axis > 2)
    throw new ArgumentOutOfRangeException(nameof(vector3Axis));
bool enabled = IsAxisEnabled(vector3Axis);

Prevention

When it happens

Trigger: Passing an axis index other than 0, 1, or 2, often from a loop that iterates beyond 2 or from a computed index that wraps or overflows.

Common situations: Custom PrimitiveBoundsHandle subclasses that override or extend axis logic using integer indices; a loop variable or bit-shift result that exceeds the valid range.

Related errors


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