{"record":{"id":"71ca59da56ede5a3","repo":"Unity-Technologies/UnityCsReference","slug":"the-object-to-remove-cannot-be-null","errorCode":null,"errorMessage":"The object to remove cannot be null.","messagePattern":"The object to remove cannot be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/BuildProfile/BuildProfile.API.cs","lineNumber":158,"sourceCode":"            }\n\n            if (Array.IndexOf(this.requiredComponents, found) > -1)\n                throw new ArgumentException($\"The component {typeof(T).Name} is required and cannot be removed from the build profile {name}.\");\n\n            AssetDatabase.RemoveObjectFromAsset(found);\n        }\n\n        /// <summary>\n        /// Removes a specific component from the build profile.\n        /// </summary>\n        /// <param name=\"objectToRemove\">Object to remove.</param>\n        /// <exception cref=\"ArgumentNullException\">objectToRemove is null.</exception>\n        /// <exception cref=\"ArgumentException\">objectToRemove is not part of the build profile.</exception>\n        public void RemoveComponent<T>(T objectToRemove) where T : UnityEngine.Object\n        {\n            if (objectToRemove == null)\n            {\n                throw new ArgumentNullException(nameof(objectToRemove), \"The object to remove cannot be null.\");\n            }\n\n            var path = AssetDatabase.GetAssetPath(this);\n            var objectToRemovePath = AssetDatabase.GetAssetPath(objectToRemove);\n\n            if (objectToRemovePath != path)\n            {\n                throw new ArgumentException($\"The object {objectToRemove} (path: {objectToRemovePath}) is not part of the build profile {path}.\");\n            }\n\n            if (Array.IndexOf(this.requiredComponents, objectToRemove) > -1)\n                throw new ArgumentException($\"The component {typeof(T).Name} is required and cannot be removed from the build profile {name}.\");\n\n            AssetDatabase.RemoveObjectFromAsset(objectToRemove);\n        }\n\n        /// <summary>\n        /// Forcibly removes the first occurrence of a component of type T from the build profile.","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/BuildProfile/BuildProfile.API.cs#L140-L176","documentation":"Thrown by the specific-object overload RemoveComponent<T>(T objectToRemove) when the passed reference is null. This is a null-guard identical in spirit to the one in AddComponent, throwing ArgumentNullException with the parameter name. Because T is constrained to UnityEngine.Object, destroyed objects (fake-null) are also caught.","triggerScenarios":"Passing a null or destroyed UnityEngine.Object to RemoveComponent(objectToRemove). Happens when a cached reference goes stale after AssetDatabase operations or when a field was never assigned.","commonSituations":"Calling RemoveComponent with a reference cached before an asset reload that has since destroyed the object; logic that does not verify the object still exists before removal; iterating a list that may contain nulls.","solutions":["Null-check objectToRemove before calling RemoveComponent.","Verify the object is still valid (e.g. objectToRemove != null) and part of the profile before attempting removal.","Use the parameterless RemoveComponent<T>() if you only know the type, not a specific instance."],"exampleFix":"// before\nprofile.RemoveComponent(maybeNullRef);\n\n// after\nif (objectToRemove != null)\n    profile.RemoveComponent(objectToRemove);","handlingStrategy":"validation","validationCode":"if (objectToRemove != null) profile.RemoveComponent(objectToRemove);","typeGuard":"static bool IsLive(UnityEngine.Object o) => o != null;","tryCatchPattern":null,"preventionTips":["Null-check (fake-null aware) before removing.","Refresh references after AssetDatabase operations.","Prefer the parameterless RemoveComponent<T>() when only the type is known."],"tags":["unity","buildprofile","null","argumentnullexception","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}