{"record":{"id":"3f9cb58078645d60","repo":"Unity-Technologies/UnityCsReference","slug":"prefab-instance-must-not-be-null","errorCode":null,"errorMessage":"Prefab instance must not be null.","messagePattern":"Prefab instance must not be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1577,"sourceCode":"        public static void ApplyRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode action)\n        {\n            DateTime startTime = DateTime.UtcNow;\n\n            ThrowExceptionIfNotValidPrefabInstanceObject(gameObjectInInstance, true);\n\n            if (assetGameObject == null)\n                throw new ArgumentNullException(nameof(assetGameObject), \"Prefab source must not be null.\");\n            if (!IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGameObject)))\n                throw new ArgumentException(\"Prefab instance must match Prefab source.\");\n            if (assetGameObject.transform.root == assetGameObject.transform)\n                throw new ArgumentException(\"The asset GameObject cannot be the root as the root cannot be removed as an override.\");\n\n            var actionName = \"Apply Prefab removed GameObject\";\n            var prefabInstanceObject = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);\n            GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);\n\n            if (prefabInstanceObject == null)\n                throw new ArgumentNullException(nameof(prefabInstanceObject), \"Prefab instance must not be null.\");\n\n            PrefabUtility.Internal_CallPrefabInstanceApplying(prefabInstanceRoot);\n\n            string assetPath = AssetDatabase.GetAssetPath(assetGameObject);\n            GameObject assetRoot = GetRootGameObject(assetGameObject);\n            byte[] originalFileContent = null;\n\n            if (action == InteractionMode.UserAction)\n            {\n                if (!FileUtil.ReadFileContentBinary(assetPath, out originalFileContent, out string errorMessage))\n                    Debug.LogError($\"No undo was registered when removing GameObject {assetGameObject.name} from {assetRoot.name}. \\nError: {errorMessage}\", assetRoot);\n            }\n\n            using (var scope = new EditPrefabContentsScope(assetPath))\n            {\n                var assetGOId = Unsupported.GetFileIDHint(assetGameObject);\n                var transformsInAsset = scope.prefabContentsRoot.GetComponentsInChildren(typeof(Transform), true);\n","sourceCodeStart":1559,"sourceCodeEnd":1595,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1559-L1595","documentation":"Thrown by ApplyRemovedGameObject when PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance) returns null. This means gameObjectInInstance is not actually part of a prefab instance in the open scene, so there is no instance handle to apply the removal override onto. The library refuses to proceed because applying a removal requires a live prefab instance context.","triggerScenarios":"Calling the apply-removed-GameObject path with a gameObjectInInstance that is a plain scene object, a disconnected/broken prefab, or an object whose instance was already reverted/unloaded. Also reachable when the selection contains an object that is not a descendant of any prefab instance root.","commonSituations":"Iterating a heterogeneous selection that mixes prefab children with loose scene objects; operating on an object whose prefab connection was severed by a prior unpack/revert; batch scripts that do not filter by prefab membership before applying overrides.","solutions":["Guard the call with PrefabUtility.GetOutermostPrefabInstanceRoot(gameObjectInInstance) != null before invoking.","Ensure gameObjectInInstance is a child of a prefab instance, not a disconnected prefab or a plain GameObject.","When batching, filter out nulls and non-prefab objects (check IsPartOfPrefabInstance) before applying."],"exampleFix":"// before\nPrefabUtility.ApplyRemovedGameObject(goInInstance, assetGo, InteractionMode.UserAction);\n\n// after\nif (PrefabUtility.GetOutermostPrefabInstanceRoot(goInInstance) != null)\n    PrefabUtility.ApplyRemovedGameObject(goInInstance, assetGo, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"if (gameObjectInInstance == null) return false;\nvar root = PrefabUtility.GetOutermostPrefabInstanceRoot(gameObjectInInstance);\nif (root == null) return false;\nvar handle = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);\nreturn handle != null;","typeGuard":"static bool IsValidPrefabInstanceObject(GameObject go) =>\n    go != null && PrefabUtility.GetOutermostPrefabInstanceRoot(go) != null\n    && PrefabUtility.GetPrefabInstanceHandle(go) != null;","tryCatchPattern":null,"preventionTips":["Always confirm the object is part of a prefab instance via GetOutermostPrefabInstanceRoot before apply/revert calls.","Filter selections through IsPartOfPrefabInstance before batch operations.","Re-resolve instance handles right before calling, since earlier resolution may be stale."],"tags":["prefab","unity","validation","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}