Unity-Technologies/UnityCsReference · error · ArgumentNullException
The object to add cannot be null.
Error message
The object to add cannot be null.
What it means
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.
Source
Thrown at Editor/Mono/BuildProfile/BuildProfile.API.cs:108
}
}
return null;
}
/// <summary>
/// Adds a given component to the build profile. If the component is not an asset in
/// the project, it will be added as a sub-asset to the build profile.
/// </summary>
/// <typeparam name="T">Component type</typeparam>
/// <param name="objectToAdd"></param>
/// <exception cref="ArgumentNullException">Thrown when objectToAdd is null.</exception>
/// <exception cref="ArgumentException">Thrown if adding a sub-asset of an existing component type.</exception>
public void AddComponent<T>(T objectToAdd) where T : UnityEngine.Object
{
if (objectToAdd == null)
{
throw new ArgumentNullException(nameof(objectToAdd), "The object to add cannot be null.");
}
// Don't allow duplicates
var found = GetComponent<T>();
if (found != null)
{
throw new ArgumentException($"The component {typeof(T).Name} already exists in the build profile {name}.");
}
var assetPath = AssetDatabase.GetAssetPath(objectToAdd);
if (string.IsNullOrEmpty(assetPath))
{
objectToAdd.hideFlags |= HideFlags.HideInHierarchy;
AssetDatabase.AddObjectToAsset(objectToAdd, this);
return;
}
// TODO: Future API will support build profile components as reference.View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before
profile.AddComponent(someField); // someField is null
// after
if (someField != null)
profile.AddComponent(someField);
else
Debug.LogError("Cannot add a null component to the build profile."); Defensive patterns
Strategy: validation
Validate before calling
if (objectToAdd == null) throw new System.ArgumentNullException(nameof(objectToAdd));
Type guard
static bool IsValid(UnityEngine.Object o) => o != null; // catches fake-null too
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The object to remove cannot be null.
- Platform GUID {platformGuid} is not a valid Unity build plat
- Active build profile is null.
- guid
- prefabInstance
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/498b1cd900041321.
Report an issue: GitHub.