{"record":{"id":"45d9e15a976445ab","repo":"CoplayDev/unity-mcp","slug":"cannot-deserialize-vector4-from-token-type-to","errorCode":null,"errorMessage":"Cannot deserialize Vector4 from {token.Type}: '{token}'","messagePattern":"Cannot deserialize Vector4 from (.+?): '(.+?)'","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Runtime/Serialization/UnityTypeConverters.cs","lineNumber":198,"sourceCode":"            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.WritePropertyName(\"w\");\n            writer.WriteValue(value.w);\n            writer.WriteEndObject();\n        }\n\n        public override Vector4 ReadJson(JsonReader reader, Type objectType, Vector4 existingValue, bool hasExistingValue, JsonSerializer serializer)\n        {\n            JToken token = JToken.Load(reader);\n            if (token is JArray arr && arr.Count >= 4)\n                return new Vector4((float)arr[0], (float)arr[1], (float)arr[2], (float)arr[3]);\n            if (token is not JObject jo)\n                throw new JsonSerializationException($\"Cannot deserialize Vector4 from {token.Type}: '{token}'\");\n            return new Vector4(\n                (float)jo[\"x\"],\n                (float)jo[\"y\"],\n                (float)jo[\"z\"],\n                (float)jo[\"w\"]\n            );\n        }\n    }\n\n    /// <summary>\n    /// Safe converter for Matrix4x4 that only accesses raw matrix elements (m00-m33).\n    /// Avoids computed properties (lossyScale, rotation, inverse) that call ValidTRS()\n    /// and can crash Unity on non-TRS matrices (common in Cinemachine components).\n    /// Fixes: https://github.com/CoplayDev/unity-mcp/issues/478\n    /// </summary>\n    public class Matrix4x4Converter : JsonConverter<Matrix4x4>\n    {\n        public override void WriteJson(JsonWriter writer, Matrix4x4 value, JsonSerializer serializer)","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Runtime/Serialization/UnityTypeConverters.cs#L180-L216","documentation":"Thrown by Vector4Converter.ReadJson when the JSON token is neither a JArray with count >= 4 nor a JObject. Vector4 accepts [x, y, z, w] array form or {\"x\":..,\"y\":..,\"z\":..,\"w\":..} object form.","triggerScenarios":"Deserializing a Vector4 field from JSON where the value is a scalar, string, or array with fewer than 4 elements. Less common than Vector3 errors since Vector4 is used for shader properties, tangents, and other specialized fields.","commonSituations":"Client sends a 3-element array where a 4-element Vector4 is expected; AI outputs a scalar for a vector field; type confusion between Vector3 and Vector4; shader property sent with wrong component count.","solutions":["Send Vector4 as [x, y, z, w] array: \"value\": [1.0, 0.0, 0.0, 0.0].","Or send as object: \"value\": {\"x\": 1.0, \"y\": 0.0, \"z\": 0.0, \"w\": 0.0}.","Ensure arrays have exactly 4 numeric elements.","Verify the target field actually expects a Vector4 and not a Vector3."],"exampleFix":"// before\nparams = {\"tangent\": [1.0, 0.0, 0.0]}\n// after\nparams = {\"tangent\": [1.0, 0.0, 0.0, 0.0]}","handlingStrategy":"type-guard","validationCode":"def is_valid_vector4(val) -> bool:\n    if isinstance(val, (list, tuple)) and len(val) >= 4:\n        return all(isinstance(v, (int, float)) for v in val[:4])\n    if isinstance(val, dict):\n        return all(k in val for k in ('x', 'y', 'z', 'w'))\n    return False","typeGuard":"function isVector4(val: unknown): val is number[] | {x:number;y:number;z:number;w:number} {\n  if (Array.isArray(val) && val.length >= 4)\n    return val.slice(0, 4).every(v => typeof v === 'number');\n  if (val && typeof val === 'object' && 'x' in val && 'y' in val && 'z' in val && 'w' in val)\n    return true;\n  return false;\n}","tryCatchPattern":"try\n{\n    var vec = serializer.Deserialize<Vector4>(reader);\n}\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Cannot deserialize Vector4\"))\n{\n    return new ErrorResponse($\"Vector4 must be [x,y,z,w] or {{x,y,z,w}}.\");\n}","preventionTips":["Verify the target field expects Vector4 (4 components), not Vector3 (3).","Send Vector4 as [x, y, z, w] arrays.","Validate component count on the client before sending."],"tags":["serialization","vector4","json","deserialization","type-converter"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}