{"record":{"id":"498b1cd900041321","repo":"Unity-Technologies/UnityCsReference","slug":"the-object-to-add-cannot-be-null","errorCode":null,"errorMessage":"The object to add cannot be null.","messagePattern":"The object to add cannot be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/BuildProfile/BuildProfile.API.cs","lineNumber":108,"sourceCode":"                }\n            }\n\n            return null;\n        }\n\n        /// <summary>\n        /// Adds a given component to the build profile. If the component is not an asset in\n        /// the project, it will be added as a sub-asset to the build profile.\n        /// </summary>\n        /// <typeparam name=\"T\">Component type</typeparam>\n        /// <param name=\"objectToAdd\"></param>\n        /// <exception cref=\"ArgumentNullException\">Thrown when objectToAdd is null.</exception>\n        /// <exception cref=\"ArgumentException\">Thrown if adding a sub-asset of an existing component type.</exception>\n        public void AddComponent<T>(T objectToAdd) where T : UnityEngine.Object\n        {\n            if (objectToAdd == null)\n            {\n                throw new ArgumentNullException(nameof(objectToAdd), \"The object to add cannot be null.\");\n            }\n\n            // Don't allow duplicates\n            var found = GetComponent<T>();\n            if (found != null)\n            {\n                throw new ArgumentException($\"The component {typeof(T).Name} already exists in the build profile {name}.\");\n            }\n\n            var assetPath = AssetDatabase.GetAssetPath(objectToAdd);\n            if (string.IsNullOrEmpty(assetPath))\n            {\n                objectToAdd.hideFlags |= HideFlags.HideInHierarchy;\n                AssetDatabase.AddObjectToAsset(objectToAdd, this);\n                return;\n            }\n\n            // TODO: Future API will support build profile components as reference.","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/BuildProfile/BuildProfile.API.cs#L90-L126","documentation":"Thrown by BuildProfile.AddComponent<T>(T objectToAdd) when the supplied objectToAdd is null. This is a standard null-guard using ArgumentNullException with the parameter name. Because T is constrained to UnityEngine.Object (which can have a 'fake null' state), the check catches both true null and destroyed-but-referenced Unity objects.","triggerScenarios":"Passing a null reference, a not-yet-assigned field, or a reference to an already-destroyed UnityEngine.Object to AddComponent. Also triggered when a ScriptableObject.CreateInstance call returned null due to an invalid type.","commonSituations":"Uninitialized fields passed into AddComponent; using an object that was destroyed by DestroyImmediate or AssetDatabase operations; deserialization producing a null; logic that assumes a creation step succeeded without checking its return value.","solutions":["Null-check objectToAdd before calling AddComponent and handle the missing-object case explicitly.","Ensure the object is actually created (e.g. ScriptableObject.CreateInstance returned non-null) before passing it in.","Investigate why the reference is null — it often indicates an earlier creation or lookup that silently failed."],"exampleFix":"// before\nprofile.AddComponent(someField); // someField is null\n\n// after\nif (someField != null)\n    profile.AddComponent(someField);\nelse\n    Debug.LogError(\"Cannot add a null component to the build profile.\");","handlingStrategy":"validation","validationCode":"if (objectToAdd == null) throw new System.ArgumentNullException(nameof(objectToAdd));","typeGuard":"static bool IsValid(UnityEngine.Object o) => o != null; // catches fake-null too","tryCatchPattern":null,"preventionTips":["Null-check UnityEngine.Object references before API calls (fake-null aware).","Validate creation results before forwarding them.","Avoid caching references that may be destroyed by AssetDatabase."],"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"}