{"record":{"id":"3c1fff9be49dae65","repo":"Unity-Technologies/UnityCsReference","slug":"prefabinstanceroots-has-no-objects","errorCode":null,"errorMessage":"prefabInstanceRoots has no objects","messagePattern":"prefabInstanceRoots has no objects","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2286,"sourceCode":"                // Recording undo does not handle missing scripts\n                var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(prefabInstanceRoot);\n                if (gameObjectsWithInvalidScript.Count > 0)\n                    throw new InvalidOperationException(string.Format($\"Cannot replace the Prefab instance when it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' has a missing script. Use InteractionMode.AutomatedAction to force the replace.\"));\n            }\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, InteractionMode mode)\n        {\n            ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, new PrefabReplacingSettings(), mode);\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, PrefabReplacingSettings settings, InteractionMode mode)\n        {\n            if (prefabInstanceRoots == null)\n                throw new ArgumentNullException(nameof(prefabInstanceRoots));\n\n            if (prefabInstanceRoots.Length == 0)\n                throw new ArgumentException(nameof(prefabInstanceRoots) + \" has no objects\");\n\n            if (settings == null)\n                throw new ArgumentNullException(nameof(settings));\n\n            ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);\n            foreach (var go in prefabInstanceRoots)\n                ThrowIfInvalidArgumentsForReplacePrefabInstance(go, prefabAssetRoot, false, mode);\n\n            foreach (var go in prefabInstanceRoots)\n                ReplacePrefabAssetOfPrefabInstance_NoInputValidation(go, prefabAssetRoot, settings, mode);\n\n            EditorUtility.ForceRebuildInspectors();\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstance(GameObject prefabInstanceRoot, GameObject prefabAssetRoot, InteractionMode mode)\n        {\n            ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, new PrefabReplacingSettings(), mode);\n        }","sourceCodeStart":2268,"sourceCodeEnd":2304,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2268-L2304","documentation":"Thrown by ReplacePrefabAssetOfPrefabInstances when prefabInstanceRoots.Length == 0. The batch API rejects an empty array because there is nothing to replace — this is a logic-error guard so callers don't silently no-op.","triggerScenarios":"Passing an empty GameObject[] to ReplacePrefabAssetOfPrefabInstances — e.g. after filtering a selection down to zero Prefab instances.","commonSituations":"Bulk-replace script that filters Selection.gameObjects for Prefab instances but the selection contained none. A serialized list that deserialized empty. A Where/Find filter whose predicate matched nothing.","solutions":["Check prefabInstanceRoots.Length > 0 before calling the API.","Verify your selection / filter actually yields Prefab instances before building the array.","Return early from your method if the filtered list is empty."],"exampleFix":"// before\nvar roots = Selection.gameObjects.Where(PrefabUtility.IsPartOfNonAssetPrefabInstance).ToArray();\nPrefabUtility.ReplacePrefabAssetOfPrefabInstances(roots, prefabAsset, mode);\n\n// after\nvar roots = Selection.gameObjects.Where(PrefabUtility.IsPartOfNonAssetPrefabInstance).ToArray();\nif (roots.Length > 0)\n    PrefabUtility.ReplacePrefabAssetOfPrefabInstances(roots, prefabAsset, mode);","handlingStrategy":"validation","validationCode":"if (prefabInstanceRoots == null || prefabInstanceRoots.Length == 0)\n    return;\nPrefabUtility.ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, mode);","typeGuard":"static bool HasAnyElements<T>(T[] array) => array != null && array.Length > 0;","tryCatchPattern":null,"preventionTips":["Always guard batch APIs with a length check.","Verify your selection/filter returns results before calling."],"tags":["prefab","empty-array","argument-validation","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}