{"record":{"id":"dcf24d6ac2bccf5a","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-apply-added-component-component-is-null","errorCode":null,"errorMessage":"Cannot apply added component. Component is null.","messagePattern":"Cannot apply added component\\. Component is null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1168,"sourceCode":"        {\n            if (originalComponentOrder.Length != newComponentOrder.Length)\n                return true;\n\n            for (int i = 0; i < originalComponentOrder.Length; ++i)\n            {\n                if (originalComponentOrder[i] != newComponentOrder[i])\n                    return true;\n            }\n\n            return false;\n        }\n\n        public static void ApplyAddedComponent(Component component, string assetPath, InteractionMode action)\n        {\n            DateTime startTime = DateTime.UtcNow;\n\n            if (component == null)\n                throw new ArgumentNullException(nameof(component), \"Cannot apply added component. Component is null.\");\n\n            if (!PrefabUtility.IsAddedComponentOverride(component))\n                throw new ArgumentException(\"Cannot apply added component. Component is not an added component override on a Prefab instance.\", nameof(component));\n\n            ThrowExceptionIfInstanceIsPersistent(component);\n\n            try\n            {\n                GameObject prefabSourceGameObject = GetCorrespondingObjectFromSourceAtPath(component.gameObject, assetPath);\n                if (prefabSourceGameObject == null)\n                    return;\n\n                var originalComponentOrder = component.gameObject.GetComponents<Component>();\n\n                var actionName = \"Apply Added Component\";\n                if (action == InteractionMode.UserAction)\n                {\n                    string dependentComponents = string.Join(","sourceCodeStart":1150,"sourceCodeEnd":1186,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1150-L1186","documentation":"Thrown by ApplyAddedComponent(Component, string, InteractionMode) when component is null — ArgumentNullException(nameof(component)). The method dereferences component immediately (IsAddedComponentOverride, gameObject access), so null is rejected with an explanatory message.","triggerScenarios":"Calling ApplyAddedComponent(null, ...); passing a Component reference that was destroyed before the call; GetComponent<T>() returning null and being forwarded.","commonSituations":"Editor menu items that apply the currently selected component when nothing valid is selected; GetComponent lookups that silently return null; deferred apply operations whose target was destroyed.","solutions":["Null-check before calling: if (component == null) return;","Verify GetComponent returned a valid component before forwarding.","Use the Unity null check (component == null) which also catches destroyed UnityEngine.Object references."],"exampleFix":"// before\nvar c = go.GetComponent<Rigidbody>();\nPrefabUtility.ApplyAddedComponent(c, assetPath, InteractionMode.UserAction);\n\n// after\nvar c = go.GetComponent<Rigidbody>();\nif (c == null) return;\nPrefabUtility.ApplyAddedComponent(c, assetPath, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"if (component == null) { Debug.LogWarning(\"Component is null; cannot apply.\"); return; }\nPrefabUtility.ApplyAddedComponent(component, assetPath, action);","typeGuard":"static bool IsLiveComponent(Component c) => c != null && c.gameObject != null;","tryCatchPattern":null,"preventionTips":["Null-check (Unity null check) any Component before apply/revert APIs.","Re-resolve components via GetComponent immediately before the call.","Guard editor menu items against empty selections."],"tags":["prefab","validation","null-argument","added-component"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}