Unity-Technologies/UnityCsReference · error · ArgumentException

List of materials contains null

Error message

List of materials contains null

What it means

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.

Source

Thrown at Editor/Mono/Inspector/MaterialEditor.cs:2314

            MaterialProperty prop = GetMaterialProperty(targets, propertyName);
            return TextureProperty(prop, label, scaleOffset);
        }

        [System.Obsolete("Use ShaderProperty that takes MaterialProperty parameter instead.")]
        public void ShaderProperty(Shader shader, int propertyIndex)
        {
            MaterialProperty prop = GetMaterialProperty(targets, propertyIndex);
            ShaderProperty(prop, prop.displayName);
        }

        // -------- other functionality

        public static MaterialProperty[] GetMaterialProperties(Object[] mats)
        {
            if (mats == null)
                throw new ArgumentNullException("mats");
            if (Array.IndexOf(mats, null) >= 0)
                throw new ArgumentException("List of materials contains null");
            return ShaderUtil.GetMaterialProperties(mats);
        }

        public static string[] GetMaterialPropertyNames(Object[] mats)
        {
            if (mats == null)
                throw new ArgumentNullException("mats");
            if (Array.IndexOf(mats, null) >= 0)
                throw new ArgumentException("List of materials contains null");
            return ShaderUtil.GetMaterialPropertyNames(mats);
        }

        public static MaterialProperty GetMaterialProperty(Object[] mats, string name)
        {
            if (mats == null)
                throw new ArgumentNullException("mats");
            if (Array.IndexOf(mats, null) >= 0)
                throw new ArgumentException("List of materials contains null");

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter nulls out before calling: mats = mats.Where(m => m != null).ToArray().
  2. Assign materials to all slots on the renderer so no element is null.
  3. Validate with Array.IndexOf(mats, null) < 0 before invoking and log/skip if not.

Example fix

// before
var props = MaterialEditor.GetMaterialProperties(renderer.sharedMaterials);

// after
var mats = renderer.sharedMaterials.Where(m => m != null).ToArray();
if (mats.Length == 0) return;
var props = MaterialEditor.GetMaterialProperties(mats);
Defensive patterns

Strategy: validation

Validate before calling

Object[] mats = renderer.sharedMaterials;
bool ok = mats != null && Array.IndexOf(mats, null) < 0;

Type guard

static Object[] FilterMaterials(Object[] mats) =>
    mats?.Where(m => m != null).ToArray() ?? Array.Empty<Object>();

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/7549cb3c92ece0ae. Report an issue: GitHub.