{"record":{"id":"2cfd887ad91fdfcd","repo":"CoplayDev/unity-mcp","slug":"color-array-must-have-3-or-4-elements","errorCode":null,"errorMessage":"Color array must have 3 or 4 elements.","messagePattern":"Color array must have 3 or 4 elements\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Helpers/MaterialOps.cs","lineNumber":382,"sourceCode":"                    return new Color(\n                        (float)jArray[0],\n                        (float)jArray[1],\n                        (float)jArray[2],\n                        (float)jArray[3]\n                    );\n                }\n                else if (jArray.Count == 3)\n                {\n                    return new Color(\n                        (float)jArray[0],\n                        (float)jArray[1],\n                        (float)jArray[2],\n                        1f\n                    );\n                }\n                else\n                {\n                    throw new ArgumentException(\"Color array must have 3 or 4 elements.\");\n                }\n            }\n\n            try\n            {\n                return token.ToObject<Color>(serializer);\n            }\n            catch (Exception ex)\n            {\n                McpLog.Warn($\"[MaterialOps] Failed to parse color from token: {ex.Message}\");\n                throw;\n            }\n        }\n    }\n}\n","sourceCodeStart":364,"sourceCodeEnd":398,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Helpers/MaterialOps.cs#L364-L398","documentation":"When parsing a Color from a JSON token, MaterialOps accepts a 4-element array [r,g,b,a] or a 3-element array [r,g,b] (alpha defaults to 1f). Any other length falls through to the final 'else' and throws ArgumentException. This enforces a strict color-array contract before delegating to the Newtonsoft serializer.","triggerScenarios":"Passing a JSON color array with 0, 1, 2, or 5+ numeric elements (e.g. [1,2] or [1,2,3,4,5]); passing nested arrays or a single number where an array is expected. The 4-case is handled above the shown source; the 3-case is shown; everything else hits the else.","commonSituations":"An AI/LLM generating material color parameters with the wrong arity; hand-built JSON with a typo or extra component; passing an HDR/linear color with an intensity suffix; confusing a hex string with an array.","solutions":["Send exactly 3 ([r,g,b]) or 4 ([r,g,b,a]) float values, each typically in 0..1.","Validate the array length client-side before calling the material color API.","If the API accepts a hex string or named color object, prefer that over a raw array."],"exampleFix":"// before\n{\"color\": [1, 0, 0, 1, 0]}\n\n// after\n{\"color\": [1, 0, 0, 1]}  // RGBA","handlingStrategy":"validation","validationCode":"// Validate the color array shape before sending to the material API.\nbool IsValidColorArray(JArray arr) => arr != null && (arr.Count == 3 || arr.Count == 4)\n    && arr.All(t => t.Type == JTokenType.Float || t.Type == JTokenType.Integer);\n\nif (!IsValidColorArray(colorArr))\n    throw new ArgumentException(\"color must be [r,g,b] or [r,g,b,a] floats.\");","typeGuard":"static bool IsColorArray(JToken t)\n{\n    if (t == null || t.Type != JTokenType.Array) return false;\n    var a = (JArray)t;\n    return (a.Count == 3 || a.Count == 4)\n        && a.All(x => x.Type == JTokenType.Float || x.Type == JTokenType.Integer);\n}","tryCatchPattern":"try { material.SetColor(prop, ParseColor(token)); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Color array\"))\n{\n    // Fall back to a default or ask the caller for a corrected color array.\n    material.SetColor(prop, Color.white);\n}","preventionTips":["Always send color arrays as exactly 3 or 4 numbers in 0..1.","Validate array length client-side before the material call.","Prefer hex strings or named color objects where the API supports them."],"tags":["material","color","json","validation","parsing"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}