{"record":{"id":"0c12eaf7536ecc9d","repo":"mrdoob/three.js","slug":"three-vector3-index-is-out-of-range-index","errorCode":null,"errorMessage":"THREE.Vector3: index is out of range: ${index}","messagePattern":"THREE\\.Vector3: index is out of range: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/math/Vector3.js","lineNumber":168,"sourceCode":"\t\treturn this;\n\n\t}\n\n\t/**\n\t * Allows to set a vector component with an index.\n\t *\n\t * @param {number} index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.\n\t * @param {number} value - The value to set.\n\t * @return {Vector3} A reference to this vector.\n\t */\n\tsetComponent( index, value ) {\n\n\t\tswitch ( index ) {\n\n\t\t\tcase 0: this.x = value; break;\n\t\t\tcase 1: this.y = value; break;\n\t\t\tcase 2: this.z = value; break;\n\t\t\tdefault: throw new Error( 'THREE.Vector3: index is out of range: ' + index );\n\n\t\t}\n\n\t\treturn this;\n\n\t}\n\n\t/**\n\t * Returns the value of the vector component which matches the given index.\n\t *\n\t * @param {number} index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.\n\t * @return {number} A vector component value.\n\t */\n\tgetComponent( index ) {\n\n\t\tswitch ( index ) {\n\n\t\t\tcase 0: return this.x;","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/mrdoob/three.js/blob/da05705fa31f02710c19772a2aa70c7454807f0c/src/math/Vector3.js#L150-L186","documentation":"Thrown by Vector3.setComponent(index, value) when index is not 0, 1, or 2. setComponent maps 0->x, 1->y, 2->z; a Vector3 has exactly three components, so indices 3 and above (or negatives) are out of range.","triggerScenarios":"Calling vec3.setComponent(3, v) or any index outside [0,2]. Looping 4 times (e.g. feeding RGBA into a Vector3). Passing a Vector4's component index into a Vector3. Negative or NaN index.","commonSituations":"Shared generic loops sized for Vector4 applied to a Vector3. Deserialization of 4-component tuples. Indexing errors in math shaders mirrored on the CPU.","solutions":["Keep the index within [0,2] for Vector3; branch or clamp first.","Use Vector4 when you genuinely have four components.","Derive loop bounds from the target vector's size.","Set fields directly (vec.x/y/z) when the index is constant."],"exampleFix":"// before: looping 4 components into a Vector3\nfor (let i = 0; i < 4; i++) v3.setComponent(i, data[i]); // i===3 throws\n\n// after: bound to 3, or use Vector4\nfor (let i = 0; i < 3; i++) v3.setComponent(i, data[i]);","handlingStrategy":"validation","validationCode":"function setComponentSafe(vec, index, value) {\n  if (index < 0 || index > 2) {\n    throw new RangeError(`Vector3 index must be 0-2, got ${index}`);\n  }\n  vec.setComponent(index, value);\n  return vec;\n}","typeGuard":"function isValidVector3Index(index) {\n  return Number.isInteger(index) && index >= 0 && index <= 2;\n}","tryCatchPattern":null,"preventionTips":["Bound indexed loops at the target vector's size (3 for Vector3).","Use Vector4 when a fourth component is required.","Prefer direct field access (vec.x/y/z) for known components."],"tags":["threejs","vector","vector3","index","bounds"],"backgroundTag":null,"analyzedSha":"da05705fa31f02710c19772a2aa70c7454807f0c","analyzedAt":"2026-08-12T22:51:37.160Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}