{"record":{"id":"5aed0f32e604f5c1","repo":"Unity-Technologies/UnityCsReference","slug":"prefab-source-must-not-be-null","errorCode":null,"errorMessage":"Prefab source must not be null.","messagePattern":"Prefab source must not be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1566,"sourceCode":"        }\n\n        private static void RemoveRemovedGameObjectOverridesWhichAreNull(Object prefabInstanceObject)\n        {\n            var removedGameObjects = PrefabUtility.GetRemovedGameObjects(prefabInstanceObject);\n#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.\n            var filteredRemovedGameObjects = (from go in removedGameObjects where go != null select go).ToArray();\n#pragma warning restore UA2001\n            PrefabUtility.SetRemovedGameObjects(prefabInstanceObject, filteredRemovedGameObjects);\n        }\n\n        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","sourceCodeStart":1548,"sourceCodeEnd":1584,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1548-L1584","documentation":"Thrown by ApplyRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode) when assetGameObject is null — ArgumentNullException(nameof(assetGameObject)). The asset GameObject identifies which child was removed in the instance; without it Unity cannot map the removal back to the source hierarchy.","triggerScenarios":"Calling ApplyRemovedGameObject with a null assetGameObject; passing a destroyed GameObject reference; forwarding a Find/GetComponent lookup that returned null.","commonSituations":"Tooling applying 'removed GameObject' overrides where the asset-side lookup failed; stale references after the asset hierarchy changed; passing the instance GameObject where the asset GameObject was expected.","solutions":["Null-check assetGameObject before calling: if (assetGameObject == null) return;","Resolve assetGameObject from a freshly loaded prefab asset before the call.","Confirm gameObjectInInstance is itself a valid prefab instance object (ThrowExceptionIfNotValidPrefabInstanceObject runs first)."],"exampleFix":"// before\nPrefabUtility.ApplyRemovedGameObject(instanceGo, assetGo, InteractionMode.UserAction);\n\n// after\nif (assetGo == null) return;\nPrefabUtility.ApplyRemovedGameObject(instanceGo, assetGo, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"if (assetGameObject == null) { Debug.LogWarning(\"Prefab source GameObject is null.\"); return; }\nPrefabUtility.ApplyRemovedGameObject(gameObjectInInstance, assetGameObject, action);","typeGuard":"static bool IsLiveAssetGameObject(GameObject assetGo)\n    => assetGo != null && EditorUtility.IsPersistent(assetGo);","tryCatchPattern":null,"preventionTips":["Resolve the asset GameObject from a freshly loaded prefab asset.","Pass the asset-side GameObject, not the instance one.","Null-check assetGameObject before calling."],"tags":["prefab","validation","null-argument","removed-gameobject"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}