{"record":{"id":"7549cb3c92ece0ae","repo":"Unity-Technologies/UnityCsReference","slug":"list-of-materials-contains-null","errorCode":null,"errorMessage":"List of materials contains null","messagePattern":"List of materials contains null","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Inspector/MaterialEditor.cs","lineNumber":2314,"sourceCode":"            MaterialProperty prop = GetMaterialProperty(targets, propertyName);\n            return TextureProperty(prop, label, scaleOffset);\n        }\n\n        [System.Obsolete(\"Use ShaderProperty that takes MaterialProperty parameter instead.\")]\n        public void ShaderProperty(Shader shader, int propertyIndex)\n        {\n            MaterialProperty prop = GetMaterialProperty(targets, propertyIndex);\n            ShaderProperty(prop, prop.displayName);\n        }\n\n        // -------- other functionality\n\n        public static MaterialProperty[] GetMaterialProperties(Object[] mats)\n        {\n            if (mats == null)\n                throw new ArgumentNullException(\"mats\");\n            if (Array.IndexOf(mats, null) >= 0)\n                throw new ArgumentException(\"List of materials contains null\");\n            return ShaderUtil.GetMaterialProperties(mats);\n        }\n\n        public static string[] GetMaterialPropertyNames(Object[] mats)\n        {\n            if (mats == null)\n                throw new ArgumentNullException(\"mats\");\n            if (Array.IndexOf(mats, null) >= 0)\n                throw new ArgumentException(\"List of materials contains null\");\n            return ShaderUtil.GetMaterialPropertyNames(mats);\n        }\n\n        public static MaterialProperty GetMaterialProperty(Object[] mats, string name)\n        {\n            if (mats == null)\n                throw new ArgumentNullException(\"mats\");\n            if (Array.IndexOf(mats, null) >= 0)\n                throw new ArgumentException(\"List of materials contains null\");","sourceCodeStart":2296,"sourceCodeEnd":2332,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Inspector/MaterialEditor.cs#L2296-L2332","documentation":"Thrown by GetMaterialProperties(Object[] mats) when the array passed in contains at least one null element. The API requires a fully populated array of Material objects before forwarding to ShaderUtil.GetMaterialProperties.","triggerScenarios":"Passing an Object[] that includes nulls, commonly a renderer.sharedMaterials / GetComponents array where a material slot is unassigned. Selecting a renderer whose material list has empty slots.","commonSituations":"Calling GetMaterialProperties(renderer.sharedMaterials) on a mesh renderer with fewer materials than slots. UI/element renderers with optional material slots. Iterating a selection set that includes destroyed materials.","solutions":["Filter nulls out before calling: mats = mats.Where(m => m != null).ToArray().","Assign materials to all slots on the renderer so no element is null.","Validate with Array.IndexOf(mats, null) < 0 before invoking and log/skip if not."],"exampleFix":"// before\nvar props = MaterialEditor.GetMaterialProperties(renderer.sharedMaterials);\n\n// after\nvar mats = renderer.sharedMaterials.Where(m => m != null).ToArray();\nif (mats.Length == 0) return;\nvar props = MaterialEditor.GetMaterialProperties(mats);","handlingStrategy":"validation","validationCode":"Object[] mats = renderer.sharedMaterials;\nbool ok = mats != null && Array.IndexOf(mats, null) < 0;","typeGuard":"static Object[] FilterMaterials(Object[] mats) =>\n    mats?.Where(m => m != null).ToArray() ?? Array.Empty<Object>();","tryCatchPattern":null,"preventionTips":["Always strip nulls from material arrays before editor APIs.","Fill all renderer material slots.","Re-query sharedMaterials fresh each call."],"tags":["unity","editor","material","null-check","renderer"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}