{"record":{"id":"abd5018073f21cb2","repo":"Unity-Technologies/UnityCsReference","slug":"input-array-contains-objects-which-are-not-part-of","errorCode":null,"errorMessage":"Input array contains objects which are not part of a Prefab instance.","messagePattern":"Input array contains objects which are not part of a Prefab instance\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":390,"sourceCode":"                AssetDatabase.StopAssetEditing();\n            }\n\n            if (action == InteractionMode.UserAction)\n                Undo.FlushTrackedObjects(); // Needs to be called after StopAssetEditing() to fix UUM-6917\n        }\n\n        public static void RemoveUnusedOverrides(GameObject[] prefabInstances, InteractionMode action)\n        {\n            if (prefabInstances == null)\n                throw new ArgumentNullException(nameof(prefabInstances));\n\n            HashSet<GameObject> rootSet = new HashSet<GameObject>(prefabInstances.Length);\n            foreach (var gameObject in prefabInstances)\n            {\n                if (gameObject == null)\n                    throw new ArgumentException(\"Input array contains null elements.\", nameof(prefabInstances));\n                if (!PrefabUtility.IsPartOfPrefabInstance(gameObject))\n                    throw new ArgumentException(\"Input array contains objects which are not part of a Prefab instance.\", nameof(prefabInstances));\n\n                rootSet.Add(PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject));\n            }\n\n#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.\n            InstanceOverridesInfo[] instanceOverridesInfos = rootSet.Select(PrefabUtility.GetPrefabInstanceOverridesInfo_Internal).ToArray();\n#pragma warning restore UA2001\n            PrefabUtility.RemovePrefabInstanceUnusedOverrides(instanceOverridesInfos, action);\n        }\n\n        internal static void RemoveAllPrefabInstancesUnusedOverridesFromSceneForMenuItem(object userData)\n        {\n            RemoveAllPrefabInstancesUnusedOverridesFromScene((Scene)userData);\n        }\n\n        static void RemoveAllPrefabInstancesUnusedOverridesFromScene(Scene scene)\n        {\n            if (!scene.IsValid())","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L372-L408","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nPrefabUtility.RemoveUnusedOverrides(allSelected, InteractionMode.UserAction);\n\n// after\nvar instances = allSelected.Where(PrefabUtility.IsPartOfPrefabInstance).ToArray();\nPrefabUtility.RemoveUnusedOverrides(instances, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"var instances = prefabInstances\n    .Where(go => go != null && PrefabUtility.IsPartOfPrefabInstance(go))\n    .ToArray();\nif (instances.Length == 0) return;\nPrefabUtility.RemoveUnusedOverrides(instances, action);","typeGuard":"static bool IsPrefabInstance(GameObject go)\n    => go != null && PrefabUtility.IsPartOfPrefabInstance(go) && !PrefabUtility.IsPartOfPrefabAsset(go);","tryCatchPattern":null,"preventionTips":["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."],"tags":["prefab","validation","type-mismatch"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}