CoplayDev/unity-mcp · error · Exception

Face index {indices[i]} out of range (0-{facesList.Count - 1

Error message

Face index {indices[i]} out of range (0-{facesList.Count - 1}).

What it means

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.

Source

Thrown at MCPForUnity/Editor/Tools/ProBuilder/ManageProBuilder.cs:342

                throw new Exception("Could not read faces from ProBuilderMesh.");

            var facesList = (System.Collections.IList)allFaces;

            if (faceIndicesToken == null)
            {
                // Return all faces when no indices specified
                var allResult = Array.CreateInstance(_faceType, facesList.Count);
                for (int i = 0; i < facesList.Count; i++)
                    allResult.SetValue(facesList[i], i);
                return allResult;
            }

            var indices = faceIndicesToken.ToObject<int[]>();
            var result = Array.CreateInstance(_faceType, indices.Length);
            for (int i = 0; i < indices.Length; i++)
            {
                if (indices[i] < 0 || indices[i] >= facesList.Count)
                    throw new Exception($"Face index {indices[i]} out of range (0-{facesList.Count - 1}).");
                result.SetValue(facesList[indices[i]], i);
            }
            return result;
        }

        internal static JObject ExtractProperties(JObject @params)
        {
            var propsToken = @params["properties"];
            if (propsToken is JObject jObj) return jObj;
            if (propsToken is JValue jVal && jVal.Type == JTokenType.String)
            {
                var parsed = JObject.Parse(jVal.ToString());
                if (parsed != null) return parsed;
            }

            // Fallback: properties might be at the top level
            return @params;
        }

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Call get_faces (without indices) first to see the actual face count and valid indices.
  2. Re-fetch face indices after any mesh topology change (extrude, subdivide, delete) before referencing them again.
  3. Validate that every index is within [0, faceCount-1] before sending the request.
  4. If you need all faces, omit faceIndices entirely — the tool returns all faces when no indices are specified.

Example fix

// before
params = {"target": "MyShape", "faceIndices": [0, 1, 50]}
// after: check count first, then pass valid indices
params = {"target": "MyShape", "faceIndices": [0, 1, 2]}  // cube has 6 faces: 0-5
Defensive patterns

Strategy: validation

Validate before calling

# Before calling a face operation, get the face count and clamp indices
# Call get_faces with no indices to discover the count, then:
def validate_face_indices(indices: list, face_count: int) -> bool:
    return all(0 <= i < face_count for i in indices)

Try / catch

try
{
    var selectedFaces = GetFacesByIndices(pbMesh, faceIndicesToken);
}
catch (Exception ex) when (ex.Message.Contains("Face index") && ex.Message.Contains("out of range"))
{
    // Parse the bound from the message and re-prompt with valid range
    return new ErrorResponse(ex.Message);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/2ab638505b6b8f0e. Report an issue: GitHub.