{"record":{"id":"d5f72decd7b22a20","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-is-null-d5f72d","errorCode":null,"errorMessage":"GameObject is null.","messagePattern":"GameObject is null\\.","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2631,"sourceCode":"\n        // Called after prefab instances in the scene have been updated\n        public delegate void PrefabInstanceUpdated(GameObject instance);\n        [AutoStaticsCleanupOnCodeReload]\n        public static PrefabInstanceUpdated prefabInstanceUpdated;\n        [AutoStaticsCleanupOnCodeReload(CleanupStrategy = CleanupStrategy.Clear)]\n        private static DelegateWithPerformanceTracker<PrefabInstanceUpdated> m_PrefabInstanceUpdated = new DelegateWithPerformanceTracker<PrefabInstanceUpdated>($\"{nameof(PrefabUtility)}.{nameof(prefabInstanceUpdated)}\");\n\n        [RequiredByNativeCode]\n        private static void Internal_CallPrefabInstanceUpdated(GameObject instance)\n        {\n            foreach (var evt in m_PrefabInstanceUpdated.UpdateAndInvoke(prefabInstanceUpdated))\n                evt(instance);\n        }\n\n        public static bool IsAddedGameObjectOverride(GameObject gameObject)\n        {\n            if (gameObject == null)\n                throw new ArgumentNullException(nameof(gameObject), \"GameObject is null.\");\n\n            Transform parent = gameObject.transform.parent;\n            if (parent == null)\n                return false;\n\n            // Can't be added to a prefab instance if the parent is not part of a prefab instance.\n            GameObject parentAsset = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(parent.gameObject);\n            if (parentAsset == null)\n                return false;\n\n            GameObject asset = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(gameObject);\n\n            // If object is not part of a prefab (but the parent is) we know it's added.\n            if (asset == null)\n                return true;\n\n            // We know now that the object is part of a prefab.\n            // If the root of that prefab, then it can't be part of the parent prefab, and must be added.","sourceCodeStart":2613,"sourceCodeEnd":2649,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2613-L2649","documentation":"PrefabUtility.IsAddedGameObjectOverride throws an ArgumentNullException when gameObject is null. This method checks whether a GameObject represents an added override within a prefab instance and requires a valid GameObject to inspect its transform hierarchy.","triggerScenarios":"Calling PrefabUtility.IsAddedGameObjectOverride(null) or passing a destroyed/missing GameObject reference.","commonSituations":"Calling this method inside a loop over children where one child was destroyed during iteration, or checking a GameObject field that was never assigned. Also common with Unity's fake-null pattern where a destroyed object appears non-null in C# but fails the Unity null check.","solutions":["Add a null check before calling: if (gameObject != null) before IsAddedGameObjectOverride.","Use Unity's null comparison pattern (gameObject != null handles destroyed objects).","When iterating child collections, validate each element before passing."],"exampleFix":"// before\nbool added = PrefabUtility.IsAddedGameObjectOverride(maybeDestroyedGo);\n// after\nif (gameObject != null)\n    bool added = PrefabUtility.IsAddedGameObjectOverride(gameObject);","handlingStrategy":"validation","validationCode":"if (gameObject != null)\n    bool isAdded = PrefabUtility.IsAddedGameObjectOverride(gameObject);","typeGuard":"static bool IsValidGameObject(GameObject go) => go != null; // Unity null check handles destroyed objects","tryCatchPattern":null,"preventionTips":["Use Unity's != null check (not ReferenceEquals) to catch destroyed objects","Validate GameObjects in loop bodies before each PrefabUtility call","Avoid iterating over child collections that may change during iteration"],"tags":["unity","prefab","argument-null","editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}