Unity-Technologies/UnityCsReference · error · ArgumentNullException

prefabInstances

Error message

prefabInstances

What it means

Thrown by RemoveUnusedOverrides(GameObject[] prefabInstances, InteractionMode) when prefabInstances is null. The method allocates a HashSet sized by prefabInstances.Length and enumerates it, so null fails immediately with ArgumentNullException(nameof(prefabInstances)).

Source

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

            {
                foreach (var instanceRoot in instanceRoots)
                {
                    ApplyPrefabInstance(instanceRoot, action, undoFlushTrackedObjects:false);
                }
            }
            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);
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Coalesce to an empty array: RemoveUnusedOverrides(prefabInstances ?? Array.Empty<GameObject>(), action).
  2. Early-return when no prefab instances are selected: if (prefabInstances == null || prefabInstances.Length == 0) return;
  3. Build the array from a validated selection and guarantee non-null.

Example fix

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

// after
var instances = GetSelectedInstances() ?? Array.Empty<GameObject>();
PrefabUtility.RemoveUnusedOverrides(instances, InteractionMode.UserAction);
Defensive patterns

Strategy: validation

Validate before calling

prefabInstances = prefabInstances ?? Array.Empty<GameObject>();
if (prefabInstances.Length == 0) return;
PrefabUtility.RemoveUnusedOverrides(prefabInstances, action);

Type guard

static bool IsNonNullableInstanceArray(GameObject[] arr)
    => arr != null && arr.All(go => go != null);

Prevention

When it happens

Trigger: Calling PrefabUtility.RemoveUnusedOverrides(null, action); passing a filtered selection array that ended up null; passing a field that was never populated.

Common situations: Cleanup-on-save editor scripts operating on a null selection; menu items invoked with no prefab instance selected; arrays sourced from a query that returned null instead of empty.

Related errors


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