mrdoob/three.js · error
THREE.Vector2: index is out of range: ${index}
Error message
THREE.Vector2: index is out of range: ${index} What it means
Thrown by Vector2.setComponent(index, value) when index is not 0 or 1. setComponent maps index 0 to x and 1 to y for generic/indexed access; a Vector2 has only two components, so any other index is out of range.
Source
Thrown at src/math/Vector2.js:173
return this;
}
/**
* Allows to set a vector component with an index.
*
* @param {number} index - The component index. `0` equals to x, `1` equals to y.
* @param {number} value - The value to set.
* @return {Vector2} A reference to this vector.
*/
setComponent( index, value ) {
switch ( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
default: throw new Error( 'THREE.Vector2: index is out of range: ' + index );
}
return this;
}
/**
* Returns the value of the vector component which matches the given index.
*
* @param {number} index - The component index. `0` equals to x, `1` equals to y.
* @return {number} A vector component value.
*/
getComponent( index ) {
switch ( index ) {
case 0: return this.x;View on GitHub (pinned to da05705fa3)
Solutions
- Restrict the index to 0 or 1 for Vector2; clamp or branch before calling setComponent.
- Use the correct vector type for your component count (Vector3 for 3, Vector4 for 4).
- When looping, drive the bound from the target vector's actual size, not a shared constant.
- Access components directly (vec.x, vec.y) when the index is known at write time.
Example fix
// before: assuming 3 components for (let i = 0; i < 3; i++) v2.setComponent(i, data[i]); // i===2 throws // after: bound to Vector2 size, or use the right type for (let i = 0; i < 2; i++) v2.setComponent(i, data[i]);
Defensive patterns
Strategy: validation
Validate before calling
function setComponentSafe(vec, index, value) {
if (index < 0 || index > 1) {
throw new RangeError(`Vector2 index must be 0 or 1, got ${index}`);
}
vec.setComponent(index, value);
return vec;
} Type guard
function isValidVector2Index(index) {
return Number.isInteger(index) && index >= 0 && index <= 1;
} Prevention
- Drive indexed loops from the target vector's size (2 for Vector2).
- Use Vector3/Vector4 when more components are needed.
- Prefer direct field access (vec.x, vec.y) for known components.
When it happens
Trigger: Calling vec2.setComponent(2, v) or any index outside [0,1]. Looping over a component count > 2. Passing an index derived from a Vector3/Vector4 component count into a Vector2. Negative or NaN indices.
Common situations: Generic math code that loops dim times and feeds the same index to vectors of different sizes. Deserialization feeding a 3- or 4-component tuple into a Vector2. Off-by-one in index calculations.
Related errors
- THREE.Vector3: index is out of range: ${index}
- THREE.Vector4: index is out of range: ${index}
- THREE.CubeCamera.updateCoordinateSystem(): Invalid coordinat
- THREE.BufferAttribute: array should be a Typed Array.
- Unable to determine texture byte length for ${format} format
AI-assisted analysis of mrdoob/three.js@da05705fa3 (2026-08-12).
Data as JSON: /api/errors/f04435af784258b1.
Report an issue: GitHub.