CoplayDev/unity-mcp · error · Exception

Edge index {idx} out of range (0-{allEdges.Count - 1}).

Error message

Edge index {idx} out of range (0-{allEdges.Count - 1}).

What it means

Thrown by ResolveEdges when an edge index in 'edgeIndices' is negative or exceeds the mesh's unique edge count. Unique edges are collected by CollectUniqueEdges from the ProBuilderMesh. The valid range is 0 to (edgeCount - 1).

Source

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

            if (edgePairsToken != null && edgePairsToken.Type == JTokenType.Array)
            {
                // Edge specification by vertex pairs: [{a: 0, b: 1}, ...]
                foreach (var pair in edgePairsToken)
                {
                    int a = pair["a"]?.Value<int>() ?? 0;
                    int b = pair["b"]?.Value<int>() ?? 0;
                    edgeList.Add(CreateEdge(a, b));
                }
            }
            else if (edgeIndicesToken != null)
            {
                // Edge specification by index into unique edges
                var allEdges = CollectUniqueEdges(pbMesh);
                var edgeIndices = edgeIndicesToken.ToObject<int[]>();
                foreach (int idx in edgeIndices)
                {
                    if (idx < 0 || idx >= allEdges.Count)
                        throw new Exception($"Edge index {idx} out of range (0-{allEdges.Count - 1}).");
                    edgeList.Add(allEdges[idx]);
                }
            }
            else
            {
                throw new Exception("edgeIndices or edges parameter is required.");
            }

            count = edgeList.Count;
            var edgeArray = Array.CreateInstance(_edgeType, edgeList.Count);
            for (int i = 0; i < edgeList.Count; i++)
                edgeArray.SetValue(edgeList[i], i);
            return edgeArray;
        }

        /// <summary>
        /// Create a typed List&lt;Edge&gt; from an Edge[] array for APIs that require IList&lt;Edge&gt;.
        /// </summary>

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Enumerate edges first (call a get/read edges operation) to discover valid edge indices and the total count.
  2. Re-fetch edge indices after any topology-modifying operation before referencing them.
  3. Consider specifying edges as vertex pairs {a, b} instead of indices, which are more stable across topology changes.
  4. Validate every index is within [0, edgeCount-1] before the call.

Example fix

// before
params = {"target": "MyShape", "edgeIndices": [0, 1, 100]}
// after: use vertex pairs which are independent of edge enumeration order
params = {"target": "MyShape", "edges": [{"a": 0, "b": 1}, {"a": 2, "b": 3}]}
Defensive patterns

Strategy: validation

Validate before calling

# Before calling an edge operation, enumerate unique edges to get the count
def validate_edge_indices(indices: list, edge_count: int) -> bool:
    return all(0 <= i < edge_count for i in indices)

# Prefer vertex-pair specification to avoid index instability

Try / catch

try
{
    var edges = ResolveEdges(pbMesh, props, out int count);
}
catch (Exception ex) when (ex.Message.Contains("Edge index") && ex.Message.Contains("out of range"))
{
    return new ErrorResponse(ex.Message);
}

Prevention

When it happens

Trigger: Passing edgeIndices referencing edges that don't exist on the mesh. Common when the mesh has fewer edges than expected, or indices are stale after topology changes. Note: edge count differs from face count — a cube has 18 unique edges, not 6.

Common situations: AI confuses face indices with edge indices; edge list collected from a different mesh state; ProBuilder's edge enumeration changed after bevel/extrude operations; off-by-one or negative index errors.

Related errors


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