stride3d/stride · error · ArgumentOutOfRangeException

There must be two and only two input values for Vector2.

Error message

There must be two and only two input values for Vector2.

What it means

Array-constructor guard on Vector2(float[] values): the values array does not contain exactly two elements, so X and Y cannot both be initialized from it. The faulting input is the values array length.

Solutions

  1. Ensure the array has exactly 2 elements before constructing (slice, pad, or truncate).
  2. Use the explicit Vector2(x, y) constructor when components are known individually.
  3. Fix the data-extraction code to produce a 2-element array (e.g. new float[]{ data[0], data[1] }).

Example fix

// before
var v = new Vector2(position); // position is float[3]
// after
var v = new Vector2(position[0], position[1]);
Defensive patterns

Strategy: validation

Validate before calling

if (values == null)
    throw new ArgumentNullException(nameof(values));
if (values.Length != 2)
    throw new ArgumentException($"Vector2 requires exactly 2 values, got {values.Length}.", nameof(values));
var v = new Vector2(values);

Try / catch

try
{
    var v = new Vector2(values);
}
catch (ArgumentOutOfRangeException ex)
{
    logger.LogError(ex, "Vector2 construction failed with {Count} values", values?.Length);
}

Prevention

When it happens

Trigger: Calling new Vector2(array) with arrays of length 0, 1, 3 or more, e.g. a 3-element position array truncated or a 4-element RGBA array passed by mistake.

Common situations: Passing vertex data flattened as xyz triples; deserializing 2D data that gained extra components; reusing a conversion helper built for Vector3 arrays; mixing 2D/3D asset pipelines.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/cf368447dbe02f78. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Vector2.cs:113

    /// <param name="y">Initial value for the Y component of the vector.</param>
    public Vector2(float x, float y)
    {
        X = x;
        Y = y;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Stride.Core.Mathematics.Vector2"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the X and Y components of the vector. This must be an array with two elements.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than two elements.</exception>
    public Vector2(float[] values)
    {
        if (values == null)
            throw new ArgumentNullException(nameof(values));
        if (values.Length != 2)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be two and only two input values for Vector2.");

        X = values[0];
        Y = values[1];
    }

    /// <summary>
    /// Gets a value indicting whether this instance is normalized.
    /// </summary>
    public bool IsNormalized
    {
        get { return MathF.Abs((X * X) + (Y * Y) - 1f) < MathUtil.ZeroTolerance; }
    }

    /// <summary>
    /// Gets or sets the component at the specified index.
    /// </summary>
    /// <value>The value of the X or Y component, depending on the index.</value>
    /// <param name="index">The index of the component to access. Use 0 for the X component and 1 for the Y component.</param>

View on GitHub (pinned to 96fad776d2)