{"record":{"id":"1c44ada9e6413a8e","repo":"Unity-Technologies/UnityCsReference","slug":"prefab-source-may-not-be-null","errorCode":null,"errorMessage":"Prefab source may not be null.","messagePattern":"Prefab source may not be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1361,"sourceCode":"                    filteredRemovedComponents.Add(assetComponent);\n                }\n            }\n\n            if (filteredRemovedComponents.Count != removedComponents.Length)\n                SetRemovedComponents(prefabInstanceObject, filteredRemovedComponents.ToArray());\n        }\n\n        // We can't use the same pattern of identifying the prefab asset via assetPath only,\n        // since when the component is removed in the instance, the only way to identify which component it is,\n        // is via the corresponding component on the asset. We find it by matching FileIds. Additionally supplying an assetPath would be redundant.\n        public static void ApplyRemovedComponent(GameObject instanceGameObject, Component assetComponent, InteractionMode action)\n        {\n            DateTime startTime = DateTime.UtcNow;\n\n            ThrowExceptionIfNotValidPrefabInstanceObject(instanceGameObject, true);\n\n            if (assetComponent == null)\n                throw new ArgumentNullException(nameof(assetComponent), \"Prefab source may not be null.\");\n\n            var actionName = \"Apply Prefab removed component\";\n\n            if (action == InteractionMode.UserAction)\n            {\n                string dependentComponents = string.Join(\n                    \", \",\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                    GetRemovedComponentDependencies(assetComponent, instanceGameObject, OverrideOperation.Apply).Select(ObjectNames.GetInspectorTitle));\n#pragma warning restore UA2001\n                if (!string.IsNullOrEmpty(dependentComponents))\n                {\n                    string error = String.Format(\n                        L10n.Tr(\"Can't apply removed component {0} because {1} component in the Prefab Asset depends on it.\"),\n                        ObjectNames.GetInspectorTitle(assetComponent),\n                        dependentComponents);\n                    EditorUtility.DisplayDialog(L10n.Tr(\"Can't apply removed component\"), error, L10n.Tr(\"OK\"));\n                    return;","sourceCodeStart":1343,"sourceCodeEnd":1379,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1343-L1379","documentation":"Thrown by ApplyRemovedComponent(GameObject instanceGameObject, Component assetComponent, InteractionMode) when assetComponent is null — ArgumentNullException(nameof(assetComponent)). The asset component is the only way to identify which component was removed from the instance (matched by FileID), so a null source makes the operation impossible.","triggerScenarios":"Calling ApplyRemovedComponent with a null assetComponent; passing a Component reference that was destroyed; forwarding a lookup (e.g. GetComponent on the asset) that returned null.","commonSituations":"Editor flows that apply 'removed component' overrides where the asset-side component lookup failed; scripts that compute assetComponent from a stale reference; passing the instance component instead of the asset component by mistake.","solutions":["Null-check assetComponent before calling: if (assetComponent == null) return;","Resolve the asset component from a fresh prefab asset load (AssetDatabase.LoadAssetAtPath) right before the call.","Ensure you pass the asset-side Component, not the instance-side one."],"exampleFix":"// before\nvar assetComp = asset.GetComponent<MyBehaviour>();\nPrefabUtility.ApplyRemovedComponent(instanceGo, assetComp, InteractionMode.UserAction);\n\n// after\nvar assetComp = asset.GetComponent<MyBehaviour>();\nif (assetComp == null) return;\nPrefabUtility.ApplyRemovedComponent(instanceGo, assetComp, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"if (assetComponent == null) { Debug.LogWarning(\"Prefab source component is null.\"); return; }\nPrefabUtility.ApplyRemovedComponent(instanceGameObject, assetComponent, action);","typeGuard":"static bool IsLiveAssetComponent(Component assetComponent)\n    => assetComponent != null && EditorUtility.IsPersistent(assetComponent);","tryCatchPattern":null,"preventionTips":["Resolve the asset-side component from a freshly loaded prefab asset.","Pass the asset Component (matched by FileID), never the instance one.","Null-check assetComponent before calling."],"tags":["prefab","validation","null-argument","removed-component"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}