Unity-Technologies/UnityCsReference · error · ArgumentException

Input array contains null elements.

Error message

Input array contains null elements.

What it means

Thrown by RemoveUnusedOverrides when the prefabInstances array is non-null but contains one or more null elements. The loop dereferences gameObject before calling IsPartOfPrefabInstance, so a destroyed/missing UnityEngine.Object reference triggers ArgumentException('Input array contains null elements.').

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:388

            finally
            {
                AssetDatabase.StopAssetEditing();
            }

            if (action == InteractionMode.UserAction)
                Undo.FlushTrackedObjects(); // Needs to be called after StopAssetEditing() to fix UUM-6917
        }

        public static void RemoveUnusedOverrides(GameObject[] prefabInstances, InteractionMode action)
        {
            if (prefabInstances == null)
                throw new ArgumentNullException(nameof(prefabInstances));

            HashSet<GameObject> rootSet = new HashSet<GameObject>(prefabInstances.Length);
            foreach (var gameObject in prefabInstances)
            {
                if (gameObject == null)
                    throw new ArgumentException("Input array contains null elements.", nameof(prefabInstances));
                if (!PrefabUtility.IsPartOfPrefabInstance(gameObject))
                    throw new ArgumentException("Input array contains objects which are not part of a Prefab instance.", nameof(prefabInstances));

                rootSet.Add(PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject));
            }

#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
            InstanceOverridesInfo[] instanceOverridesInfos = rootSet.Select(PrefabUtility.GetPrefabInstanceOverridesInfo_Internal).ToArray();
#pragma warning restore UA2001
            PrefabUtility.RemovePrefabInstanceUnusedOverrides(instanceOverridesInfos, action);
        }

        internal static void RemoveAllPrefabInstancesUnusedOverridesFromSceneForMenuItem(object userData)
        {
            RemoveAllPrefabInstancesUnusedOverridesFromScene((Scene)userData);
        }

        static void RemoveAllPrefabInstancesUnusedOverridesFromScene(Scene scene)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter nulls before the call: prefabInstances = prefabInstances.Where(go => go != null).ToArray();
  2. Use the Unity null-aware check (go == null catches destroyed Objects) when filtering.
  3. Rebuild the array from fresh PrefabUtility.GetOutermostPrefabInstanceRoot calls right before invoking.

Example fix

// before
PrefabUtility.RemoveUnusedOverrides(selected, InteractionMode.UserAction);

// after
var clean = selected.Where(go => go != null).ToArray();
if (clean.Length == 0) return;
PrefabUtility.RemoveUnusedOverrides(clean, InteractionMode.UserAction);
Defensive patterns

Strategy: validation

Validate before calling

var clean = prefabInstances.Where(go => go != null).ToArray();
if (clean.Length == 0) return;
PrefabUtility.RemoveUnusedOverrides(clean, action);

Type guard

static GameObject[] FilterLiveInstances(GameObject[] arr)
    => (arr ?? Array.Empty<GameObject>()).Where(go => go != null).ToArray();

Prevention

When it happens

Trigger: Passing an array built from Selection.gameObjects after some objects were destroyed; arrays holding references to deleted GameObjects; arrays assembled from a dictionary whose values were culled mid-frame.

Common situations: Editor automation that collects prefab roots and one gets destroyed before the call runs; selection arrays retained across a scene close that nulled some entries; pooled arrays reused without clearing destroyed refs.

Related errors


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