Unity-Technologies/UnityCsReference · error · ArgumentException
GameObject in input 'gameObjects' array is null
Error message
GameObject in input 'gameObjects' array is null
What it means
Thrown inside the AddGameObjectsToPrefabAndConnect loop when an individual element `go` in the gameObjects array is null. Each array element is null-checked before any prefab logic runs on it, so a single null entry aborts the whole operation.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.bindings.cs:292
throw new ArgumentNullException("gameObjects");
if (gameObjects.Length == 0)
throw new ArgumentException("gameObjects array is empty");
if (targetPrefab == null)
throw new ArgumentNullException("targetPrefab");
if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab))
throw new ArgumentException("Target Prefab has to be a Prefab Asset");
Object targetPrefabInstance = null;
var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);
foreach (GameObject go in gameObjects)
{
if (go == null)
throw new ArgumentException("GameObject in input 'gameObjects' array is null");
if (EditorUtility.IsPersistent(go)) // Prefab asset
throw new ArgumentException("Game object is part of a prefab");
var parentPrefabInstance = GetParentPrefabInstance(go);
if (parentPrefabInstance == null)
throw new ArgumentException("GameObject is not (directly) parented under a target Prefab instance.");
if (targetPrefabInstance == null)
{
targetPrefabInstance = parentPrefabInstance;
if (!IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject))
throw new ArgumentException("GameObject is not parented under a target Prefab instance.");
}
else
{
if (parentPrefabInstance != targetPrefabInstance)
{View on GitHub (pinned to 225b0fbdb5)
Solutions
- Filter nulls before calling: gos = gos.Where(g => g != null).ToArray().
- Ensure no destroyed/null references are added to the list.
- Validate each element in a loop and report which index was null for debugging.
Example fix
// before GameObject[] gos = Selection.gameObjects; // may contain nulls from editor state PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // after GameObject[] gos = Selection.gameObjects.Where(g => g != null).ToArray(); PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);
Defensive patterns
Strategy: validation
Validate before calling
gameObjects = gameObjects.Where(g => g != null).ToArray();
Prevention
- Filter nulls out of any array before passing it to bulk prefab APIs.
- Avoid pre-allocating arrays larger than needed; use List<T> and ToArray().
- Log which index was null during selection building to catch upstream bugs.
When it happens
Trigger: An array containing a null slot, e.g. from Selection.transforms converted to GameObjects where a transform was missing, a List with Add(null), or an array sized larger than its filled entries.
Common situations: Building the array from destroyed objects, Find calls returning null, mixed valid/invalid selection, or LINQ that preserves nulls. Arrays pre-allocated with `new GameObject[n]` then partially filled.
Related errors
- gameObjects
- gameObjects array is empty
- targetPrefab
- Target Prefab has to be a Prefab Asset
- Game object is part of a prefab
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/bd7e93ea5e9b60ac.
Report an issue: GitHub.