{"record":{"id":"19fc25f70f905ebd","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-revert-added-gameobject-gameobject-is-null","errorCode":null,"errorMessage":"Cannot revert added GameObject. GameObject is null.","messagePattern":"Cannot revert added GameObject\\. GameObject is null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1799,"sourceCode":"                throw new ArgumentException(\"Prefab instance should 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 = \"Revert Prefab removed GameObject\";\n            var prefabInstanceHandle = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);\n\n            GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);\n\n            if (action == InteractionMode.UserAction)\n                Undo.RegisterFullObjectHierarchyUndo(prefabInstanceRoot, actionName);\n\n            RemoveRemovedGameObjectOverride(prefabInstanceHandle, assetGameObject);\n        }\n\n        public static void RevertAddedGameObject(GameObject gameObject, InteractionMode action)\n        {\n            if (gameObject == null)\n                throw new ArgumentNullException(nameof(gameObject), \"Cannot revert added GameObject. GameObject is null.\");\n\n            if (!IsAddedGameObjectOverride(gameObject))\n                throw new ArgumentException(\"Cannot revert added GameObject. GameObject is not an added GameObject override on a Prefab instance.\", nameof(gameObject));\n\n            ThrowExceptionIfInstanceIsPersistent(gameObject);\n\n            GameObject instance = PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject.transform.parent);\n            PrefabUtility.Internal_CallPrefabInstanceReverting(instance);\n\n            if (action == InteractionMode.UserAction)\n                Undo.DestroyObjectImmediate(gameObject);\n            else\n                Object.DestroyImmediate(gameObject);\n\n            PrefabUtility.Internal_CallPrefabInstanceReverted(instance);\n        }\n\n        public static List<ObjectOverride> GetObjectOverrides(GameObject prefabInstance, bool includeDefaultOverrides = false)","sourceCodeStart":1781,"sourceCodeEnd":1817,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1781-L1817","documentation":"RevertAddedGameObject requires a non-null gameObject. The method immediately inspects the object (IsAddedGameObjectOverride), so a null reference cannot be processed and ArgumentNullException is thrown.","triggerScenarios":"Calling RevertAddedGameObject(null, action); passing a destroyed or uninitialized reference.","commonSituations":"Iterating a collection of additions where some references were destroyed between capture and revert; editor scripts operating on stale selection data.","solutions":["Null-check gameObject before calling.","Refresh the additions list immediately before reverting using GetAddedGameObjects.","Skip/log null entries instead of forwarding them."],"exampleFix":"// before\nPrefabUtility.RevertAddedGameObject(go, mode);\n\n// after\nif (go != null)\n    PrefabUtility.RevertAddedGameObject(go, mode);","handlingStrategy":"validation","validationCode":"if (gameObject == null) return;","typeGuard":"static bool IsLiveGameObject(GameObject go) => go != null && go != null;","tryCatchPattern":null,"preventionTips":["Null-check the GameObject before reverting.","Refresh the additions list with GetAddedGameObjects right before reverting to avoid destroyed references.","Skip and log null entries rather than forwarding them."],"tags":["prefab","unity","null-argument","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}