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
- Filter to prefab-instance members only: prefabInstances = prefabInstances.Where(PrefabUtility.IsPartOfPrefabInstance).ToArray();
- Confirm with PrefabUtility.GetPrefabInstanceHandle(go) != null before including the object.
- 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
- Exclude prefab Asset roots using PrefabUtility.IsPartOfPrefabAsset before passing to instance-only APIs.
- Confirm PrefabUtility.GetPrefabInstanceHandle(go) != null for each element.
- Bulk tools should filter to prefab-instance members before operating.
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
- Cannot apply added component. Component is not an added comp
- Cannot revert added component. Component is not an added com
- guid
- prefabInstance
- Provided GameObject is not a Prefab instance
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/abd5018073f21cb2.
Report an issue: GitHub.