Unity-Technologies/UnityCsReference · error · ArgumentException

Input array contains objects which are not part of a Prefab

Error message

Input array contains objects which are not part of a Prefab instance.

What it means

Thrown by RemoveUnusedOverrides when an array element is non-null but PrefabUtility.IsPartOfPrefabInstance(gameObject) returns false. The operation only makes sense on objects that are actually overrides of a prefab instance, so a plain scene object or a prefab asset root is rejected with ArgumentException.

Source

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

                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)
        {
            if (!scene.IsValid())

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter to prefab-instance members only: prefabInstances = prefabInstances.Where(PrefabUtility.IsPartOfPrefabInstance).ToArray();
  2. Confirm with PrefabUtility.GetPrefabInstanceHandle(go) != null before including the object.
  3. Distinguish prefab assets from instances using PrefabUtility.IsPartOfPrefabAsset and exclude assets.

Example fix

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

// after
var instances = allSelected.Where(PrefabUtility.IsPartOfPrefabInstance).ToArray();
PrefabUtility.RemoveUnusedOverrides(instances, InteractionMode.UserAction);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool IsPrefabInstance(GameObject go)
    => go != null && PrefabUtility.IsPartOfPrefabInstance(go) && !PrefabUtility.IsPartOfPrefabAsset(go);

Prevention

When it happens

Trigger: Passing arbitrary scene GameObjects that are not prefab instances; passing prefab Asset roots (objects inside a .prefab file) rather than instance copies; passing newly created objects not yet connected to a prefab.

Common situations: Bulk menu commands that run over the whole hierarchy including non-prefab objects; scripts that grab Selection.gameObjects without confirming each is a prefab instance; mixing prefab assets and instances in one array.

Related errors


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