{"record":{"id":"3c65454c8ef4f0e3","repo":"Unity-Technologies/UnityCsReference","slug":"the-input-instanceroot-was-destroyed-in-the-pref","errorCode":null,"errorMessage":"The input 'instanceRoot' was destroyed in the prefabInstanceUnpacking callback","messagePattern":"The input 'instanceRoot' was destroyed in the prefabInstanceUnpacking callback","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2805,"sourceCode":"            else\n            {\n                UnpackPrefabInstanceAndReturnNewOutermostRoots(instanceRoot, unpackMode);\n            }\n\n            if (action == InteractionMode.UserAction)\n                Undo.FlushTrackedObjects();\n        }\n\n        public static GameObject[] UnpackPrefabInstanceAndReturnNewOutermostRoots(GameObject instanceRoot, PrefabUnpackMode unpackMode)\n        {\n            if (instanceRoot == null)\n                throw new ArgumentNullException(nameof(instanceRoot));\n\n            prefabInstanceUnpacking?.Invoke(instanceRoot, unpackMode);\n\n            // The user can delete the instance in the prefabInstanceUnpacking callback\n            if (instanceRoot == null)\n                throw new InvalidOperationException($\"The input '{nameof(instanceRoot)}' was destroyed in the prefabInstanceUnpacking callback\");\n\n            var newRoots = UnpackPrefabInstanceAndReturnNewOutermostRoots_internal(instanceRoot, unpackMode);\n            prefabInstanceUnpacked?.Invoke(instanceRoot, unpackMode);\n\n            return newRoots;\n        }\n\n        public static void UnpackAllInstancesOfPrefab(GameObject prefabRoot, PrefabUnpackMode unpackMode, InteractionMode action)\n        {\n            var prefabInstances = FindAllInstancesOfPrefab(prefabRoot);\n            foreach (var prefabInstance in prefabInstances)\n            {\n                UnpackPrefabInstance(prefabInstance, unpackMode, action);\n            }\n        }\n\n        internal static List<GameObject> FindGameObjectsWithInvalidComponent(GameObject rootOfSearch)\n        {","sourceCodeStart":2787,"sourceCodeEnd":2823,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2787-L2823","documentation":"PrefabUtility.UnpackPrefabInstanceAndReturnNewOutermostRoots throws an InvalidOperationException when the instanceRoot GameObject was destroyed during the prefabInstanceUnpacking callback. A subscriber to the prefabInstanceUnpacking event deleted the object, making the unpack operation impossible.","triggerScenarios":"An event handler subscribed to PrefabUtility.prefabInstanceUnpacking destroys or destroys the instanceRoot GameObject during the callback. After the callback returns, the method checks if instanceRoot survived and throws if it did not.","commonSituations":"Custom editor code subscribes to prefabInstanceUnpacking for logging or cleanup and inadvertently destroys the instance (e.g., calling DestroyImmediate, or triggering a scene reload). Also happens with plugins/asset store tools that hook into prefab events aggressively.","solutions":["Audit all subscribers to PrefabUtility.prefabInstanceUnpacking and ensure none destroy instanceRoot.","If you need to clean up related objects, do so after the unpack completes (subscribe to prefabInstanceUnpacked instead).","Use Object.Destroy (deferred) instead of Object.DestroyImmediate in prefab event callbacks.","Wrap the UnpackPrefabInstance call in try-catch for InvalidOperationException and log which callback caused the destruction."],"exampleFix":"// before\nPrefabUtility.prefabInstanceUnpacking += (root, mode) =>\n{\n    // BUG: this destroys the instance mid-operation\n    if (someCondition) Object.DestroyImmediate(root);\n};\n// after\nPrefabUtility.prefabInstanceUnpacking += (root, mode) =>\n{\n    // Do NOT destroy root here; defer or handle in unpacked callback\n    _pendingCleanup.Add(root);\n};\nPrefabUtility.prefabInstanceUnpacked += (root, mode) =>\n{\n    // Safe to clean up related objects now\n};","handlingStrategy":"try-catch","validationCode":"// Cannot validate via pre-check; the destruction happens in a callback at runtime.\n// Audit subscribers to PrefabUtility.prefabInstanceUnpacking.","typeGuard":null,"tryCatchPattern":"try\n{\n    PrefabUtility.UnpackPrefabInstanceAndReturnNewOutermostRoots(instanceRoot, mode);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"prefabInstanceUnpacking\"))\n{\n    Debug.LogError($\"InstanceRoot destroyed during unpacking callback: {ex.Message}\");\n    // Audit and fix the subscriber that destroyed the object\n}","preventionTips":["Never call DestroyImmediate on instanceRoot in prefabInstanceUnpacking callbacks","Audit all event subscribers for destructive side effects","Use prefabInstanceUnpacked for cleanup tasks","Use deferred Destroy instead of DestroyImmediate in prefab callbacks"],"tags":["unity","prefab","event-callback","invalid-operation","editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}