Unity-Technologies/UnityCsReference · error · ArgumentNullException
Cannot apply added GameObjects. GameObjects array is null.
Error message
Cannot apply added GameObjects. GameObjects array is null.
What it means
ApplyAddedGameObjects throws ArgumentNullException when the gameObjects array is null. The method needs at least one GameObject to apply and dereferences the array immediately, so a null array cannot be processed. This is a hard precondition enforced at the API boundary.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:1647
action,
startTime,
false
);
PrefabUtility.Internal_CallPrefabInstanceApplied(prefabInstanceRoot);
}
public static void ApplyAddedGameObject(GameObject gameObject, string assetPath, InteractionMode action)
{
ApplyAddedGameObjects(new GameObject[] { gameObject}, assetPath, action);
}
public static void ApplyAddedGameObjects(GameObject[] gameObjects, string assetPath, InteractionMode action)
{
DateTime startTime = DateTime.UtcNow;
if (gameObjects == null)
throw new ArgumentNullException(nameof(gameObjects), "Cannot apply added GameObjects. GameObjects array is null.");
if (gameObjects.Length == 0)
throw new ArgumentException(nameof(gameObjects), "No GameObjects in array.");
foreach (GameObject go in gameObjects)
{
if (go == null)
throw new ArgumentException(nameof(go), "Input GameObject is null.");
if (!IsAddedGameObjectOverride(go))
throw new ArgumentException(nameof(go), $"Cannot apply added GameObject. GameObject '{go.name}' is not an added GameObject override on a Prefab instance.");
ThrowExceptionIfInstanceIsPersistent(go);
}
if (gameObjects.Length > 1 && !HasSameParent(gameObjects))
throw new ArgumentException(nameof(gameObjects), "ApplyAddedGameObjects requires that GameObjects share the same parent.");
View on GitHub (pinned to 225b0fbdb5)
Solutions
- Pass a non-null GameObject[]; default to Array.Empty<GameObject>() when the set is empty.
- Guard the call site: if (gameObjects != null && gameObjects.Length > 0) ApplyAddedGameObjects(...).
- Coalesce query results: (selection ?? Array.Empty<GameObject>()).
Example fix
// before
PrefabUtility.ApplyAddedGameObjects(null, path, InteractionMode.UserAction);
// after
var gos = selection ?? Array.Empty<GameObject>();
if (gos.Length > 0)
PrefabUtility.ApplyAddedGameObjects(gos, path, InteractionMode.UserAction); Defensive patterns
Strategy: validation
Validate before calling
if (gameObjects == null) return; // or coalesce: gameObjects = gameObjects ?? Array.Empty<GameObject>();
Type guard
static bool IsNonEmptyArray(GameObject[] gos) => gos != null && gos.Length > 0;
Prevention
- Coalesce query results with ?? Array.Empty<GameObject>() to never pass null.
- Guard array arguments at every public entry point that forwards to ApplyAddedGameObjects.
- Prefer List<GameObject> built explicitly over fixed-size arrays with possible null slots.
When it happens
Trigger: Calling PrefabUtility.ApplyAddedGameObjects(null, assetPath, action) directly, or passing a variable that was never initialized. Indirectly triggered when a LINQ/component query returns null instead of an empty array.
Common situations: Querying GetComponentsInChildren with a cast that yields null; forgetting to initialize an array field; chained calls where an intermediate step produced null.
Related errors
- Cannot revert added GameObject. GameObject is null.
- Parameter prefabAssetGameObject is null
- guid
- prefabInstance
- Provided GameObject is not a Prefab instance
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/e4b912439688768b.
Report an issue: GitHub.