{"record":{"id":"3f6601dfd76625e5","repo":"CoplayDev/unity-mcp","slug":"cannot-deserialize-vector3-from-token-type-to","errorCode":null,"errorMessage":"Cannot deserialize Vector3 from {token.Type}: '{token}'","messagePattern":"Cannot deserialize Vector3 from (.+?): '(.+?)'","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Runtime/Serialization/UnityTypeConverters.cs","lineNumber":32,"sourceCode":"        public override void WriteJson(JsonWriter writer, Vector3 value, JsonSerializer serializer)\n        {\n            writer.WriteStartObject();\n            writer.WritePropertyName(\"x\");\n            writer.WriteValue(value.x);\n            writer.WritePropertyName(\"y\");\n            writer.WriteValue(value.y);\n            writer.WritePropertyName(\"z\");\n            writer.WriteValue(value.z);\n            writer.WriteEndObject();\n        }\n\n        public override Vector3 ReadJson(JsonReader reader, Type objectType, Vector3 existingValue, bool hasExistingValue, JsonSerializer serializer)\n        {\n            JToken token = JToken.Load(reader);\n            if (token is JArray arr && arr.Count >= 3)\n                return new Vector3((float)arr[0], (float)arr[1], (float)arr[2]);\n            if (token is not JObject jo)\n                throw new JsonSerializationException($\"Cannot deserialize Vector3 from {token.Type}: '{token}'\");\n            return new Vector3(\n                (float)jo[\"x\"],\n                (float)jo[\"y\"],\n                (float)jo[\"z\"]\n            );\n        }\n    }\n\n    public class Vector2Converter : JsonConverter<Vector2>\n    {\n        public override void WriteJson(JsonWriter writer, Vector2 value, JsonSerializer serializer)\n        {\n            writer.WriteStartObject();\n            writer.WritePropertyName(\"x\");\n            writer.WriteValue(value.x);\n            writer.WritePropertyName(\"y\");\n            writer.WriteValue(value.y);\n            writer.WriteEndObject();","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Runtime/Serialization/UnityTypeConverters.cs#L14-L50","documentation":"Thrown by Vector3Converter.ReadJson when the JSON token being deserialized is neither a JArray with count >= 3 nor a JObject. Vector3 accepts [x, y, z] array form or {\"x\":..,\"y\":..,\"z\":..} object form; any other JSON structure (number, string, boolean, array with < 3 elements) triggers this.","triggerScenarios":"Deserializing a JSON payload containing a Vector3 field where the value is a single number (e.g. position: 5), a string (e.g. \"1,2,3\"), an array with fewer than 3 elements, or a different JSON type. Happens when the source data format doesn't match the converter's expectations.","commonSituations":"Client sends position as a comma-separated string instead of an array/object; AI model outputs a scalar where a vector is expected; serialization library omits null/default components producing shorter arrays; mismatch between Python-side dict and expected Unity vector format.","solutions":["Send Vector3 as [x, y, z] array: \"position\": [1.0, 2.0, 3.0].","Or send as object: \"position\": {\"x\": 1.0, \"y\": 2.0, \"z\": 3.0}.","Ensure arrays have exactly 3 numeric elements (floats or ints).","Do not pass strings, numbers, or booleans where a vector is expected."],"exampleFix":"// before\nparams = {\"position\": \"1,2,3\"}\n// after\nparams = {\"position\": [1.0, 2.0, 3.0]}","handlingStrategy":"type-guard","validationCode":"def is_valid_vector3(val) -> bool:\n    if isinstance(val, (list, tuple)) and len(val) >= 3:\n        return all(isinstance(v, (int, float)) for v in val[:3])\n    if isinstance(val, dict):\n        return all(k in val for k in ('x', 'y', 'z'))\n    return False","typeGuard":"function isVector3(val: unknown): val is number[] | {x:number;y:number;z:number} {\n  if (Array.isArray(val) && val.length >= 3)\n    return val.slice(0, 3).every(v => typeof v === 'number');\n  if (val && typeof val === 'object' && 'x' in val && 'y' in val && 'z' in val)\n    return true;\n  return false;\n}","tryCatchPattern":"try\n{\n    var vec = serializer.Deserialize<Vector3>(reader);\n}\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Cannot deserialize Vector3\"))\n{\n    return new ErrorResponse($\"Vector3 must be [x,y,z] or {{x,y,z}}. Got: {token}\");\n}","preventionTips":["Send Vector3 as [x, y, z] arrays with exactly 3 numeric elements.","Avoid comma-separated strings for vector fields.","Use a serialization helper to build vector payloads consistently.","Validate vector shape on the client before sending."],"tags":["serialization","vector3","json","deserialization","type-converter"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}