{"record":{"id":"2ab638505b6b8f0e","repo":"CoplayDev/unity-mcp","slug":"face-index-indices-i-out-of-range-0-faceslist","errorCode":null,"errorMessage":"Face index {indices[i]} out of range (0-{facesList.Count - 1}).","messagePattern":"Face index (.+?) out of range \\(0-(.+?)\\)\\.","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Tools/ProBuilder/ManageProBuilder.cs","lineNumber":342,"sourceCode":"                throw new Exception(\"Could not read faces from ProBuilderMesh.\");\n\n            var facesList = (System.Collections.IList)allFaces;\n\n            if (faceIndicesToken == null)\n            {\n                // Return all faces when no indices specified\n                var allResult = Array.CreateInstance(_faceType, facesList.Count);\n                for (int i = 0; i < facesList.Count; i++)\n                    allResult.SetValue(facesList[i], i);\n                return allResult;\n            }\n\n            var indices = faceIndicesToken.ToObject<int[]>();\n            var result = Array.CreateInstance(_faceType, indices.Length);\n            for (int i = 0; i < indices.Length; i++)\n            {\n                if (indices[i] < 0 || indices[i] >= facesList.Count)\n                    throw new Exception($\"Face index {indices[i]} out of range (0-{facesList.Count - 1}).\");\n                result.SetValue(facesList[indices[i]], i);\n            }\n            return result;\n        }\n\n        internal static JObject ExtractProperties(JObject @params)\n        {\n            var propsToken = @params[\"properties\"];\n            if (propsToken is JObject jObj) return jObj;\n            if (propsToken is JValue jVal && jVal.Type == JTokenType.String)\n            {\n                var parsed = JObject.Parse(jVal.ToString());\n                if (parsed != null) return parsed;\n            }\n\n            // Fallback: properties might be at the top level\n            return @params;\n        }","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Tools/ProBuilder/ManageProBuilder.cs#L324-L360","documentation":"Thrown by GetFacesByIndices when one or more values in the 'faceIndices' array are negative or exceed the ProBuilderMesh's actual face count. The valid range is 0 to (faceCount - 1). The message includes the offending index and the valid upper bound.","triggerScenarios":"Passing faceIndices containing an index that is >= the number of faces on the mesh, or a negative value. Often caused by stale face indices after the mesh was modified (faces added/removed), or by assuming a shape has more faces than it does.","commonSituations":"AI assumes a default Cube has N faces but ProBuilder subdivides differently; indices cached from a prior mesh edit are now out of date; off-by-one when computing indices programmatically; negative index passed by mistake.","solutions":["Call get_faces (without indices) first to see the actual face count and valid indices.","Re-fetch face indices after any mesh topology change (extrude, subdivide, delete) before referencing them again.","Validate that every index is within [0, faceCount-1] before sending the request.","If you need all faces, omit faceIndices entirely — the tool returns all faces when no indices are specified."],"exampleFix":"// before\nparams = {\"target\": \"MyShape\", \"faceIndices\": [0, 1, 50]}\n// after: check count first, then pass valid indices\nparams = {\"target\": \"MyShape\", \"faceIndices\": [0, 1, 2]}  // cube has 6 faces: 0-5","handlingStrategy":"validation","validationCode":"# Before calling a face operation, get the face count and clamp indices\n# Call get_faces with no indices to discover the count, then:\ndef validate_face_indices(indices: list, face_count: int) -> bool:\n    return all(0 <= i < face_count for i in indices)","typeGuard":null,"tryCatchPattern":"try\n{\n    var selectedFaces = GetFacesByIndices(pbMesh, faceIndicesToken);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"Face index\") && ex.Message.Contains(\"out of range\"))\n{\n    // Parse the bound from the message and re-prompt with valid range\n    return new ErrorResponse(ex.Message);\n}","preventionTips":["Always enumerate faces first to get the actual count.","Re-fetch face indices after every topology-changing operation.","Omit faceIndices to operate on all faces if that's the intent.","Treat returned indices as ephemeral — don't cache across edits."],"tags":["probuilder","faces","index-out-of-range","validation","bounds-check"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}