stride3d/stride · error · ArgumentOutOfRangeException
There must be four and only four input values for…
Error message
There must be four and only four input values for Quaternion.
What it means
Quaternion's float[] constructor requires exactly 4 elements (X, Y, Z, W). An array of any other length triggers ArgumentOutOfRangeException because a quaternion is defined by precisely these four components.
Solutions
- Pass an array with exactly 4 floats (X, Y, Z, W)
- Check values.Length == 4 before construction and handle otherwise
- Use the Quaternion(x, y, z, w) constructor to avoid array length ambiguity
Example fix
// before var q = new Quaternion(vec3Array); // float[3] // after var q = new Quaternion(x, y, z, w);
Defensive patterns
Strategy: validation
Validate before calling
if (values == null || values.Length != 4) throw new ArgumentException("Quaternion requires exactly 4 floats: X, Y, Z, W");
var q = new Quaternion(values); Type guard
bool IsValidQuaternionArray(float[] v) => v != null && v.Length == 4;
Try / catch
try { var q = new Quaternion(values); }
catch (ArgumentOutOfRangeException ex) { /* ex.ParamName == "values"; handle wrong-length array */ } Prevention
- Never reuse 3-component vector arrays as quaternion data
- Assert length 4 at deserialization boundaries
- Use the Quaternion(x, y, z, w) constructor for clarity
When it happens
Trigger: Calling new Quaternion(float[] values) with an array whose Length is not 4, e.g. a 3-element vector array or a rotation matrix row.
Common situations: Reusing vector (x,y,z) arrays for quaternions; parsing rotation data that includes an extra angle; truncated deserialization of quaternion payloads.
Related errors
- Indices for ColorBGRA run from 0 to 3, inclusive.
- There must be three and only three input values for Double3.
- The destination array must be of same length or larger…
- There must be four and only four input values for Double4.
- The destination array must be of same length or larger…
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/a8448efe809f78a0.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core.Mathematics/Quaternion.cs:162
{
X = x;
Y = y;
Z = z;
W = w;
}
/// <summary>
/// Initializes a new instance of the <see cref="Stride.Core.Mathematics.Quaternion"/> struct.
/// </summary>
/// <param name="values">The values to assign to the X, Y, Z, and W components of the quaternion. This must be an array with four 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 four elements.</exception>
public Quaternion(float[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.Length != 4)
throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Quaternion.");
X = values[0];
Y = values[1];
Z = values[2];
W = values[3];
}
/// <summary>
/// Gets a value indicating whether this instance is equivalent to the identity quaternion.
/// </summary>
/// <value>
/// <c>true</c> if this instance is an identity quaternion; otherwise, <c>false</c>.
/// </value>
public bool IsIdentity
{
get { return this.Equals(Identity); }
}
View on GitHub (pinned to 96fad776d2)