Unity-Technologies/UnityCsReference · error · ArgumentNullException
gameObjects
Error message
gameObjects
What it means
Thrown by AddGameObjectsToPrefabAndConnect when the `gameObjects` array argument is null. The method connects existing scene GameObjects into a target Prefab, so a null array is a programmer error caught as ArgumentNullException before any work begins.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.bindings.cs:274
[StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
extern private static GameObject ApplyPrefabInstance_Internal([NotNull] GameObject root);
[StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
[NativeMethod(ThrowsException = true)]
extern private static GameObject SaveAsPrefabAsset_Internal([NotNull] GameObject root, string path, out bool success);
[StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
[NativeMethod(ThrowsException = true)]
extern private static GameObject SaveAsPrefabAssetAndConnect_Internal([NotNull] GameObject root, string path, out bool success);
[StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
[NativeMethod(ThrowsException = true)]
extern private static GameObject SavePrefabAsset_Internal([NotNull] GameObject root, out bool success);
internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab)
{
if (gameObjects == null)
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");View on GitHub (pinned to 225b0fbdb5)
Solutions
- Pass a non-null GameObject[] even if empty (though empty throws separately).
- Initialize arrays to empty before populating: new GameObject[0].
- Guard the selection: if (gameObjects == null) return; or inform the user to select objects.
Example fix
// before PrefabUtility.AddGameObjectsToPrefabAndConnect(null, target); // after GameObject[] gos = Selection.gameObjects ?? Array.Empty<GameObject>(); if (gos.Length == 0) return; PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);
Defensive patterns
Strategy: validation
Validate before calling
if (gameObjects == null)
gameObjects = System.Array.Empty<GameObject>(); Prevention
- Initialize arrays/lists before populating rather than leaving them null.
- Coalesce selection results with `?? Array.Empty<T>()`.
- Guard null arrays upstream in your editor tool's entry points.
When it happens
Trigger: Calling AddGameObjectsToPrefabAndConnect(null, targetPrefab), or passing a field/variable that was never populated (e.g. a Selection.transforms array that returned nothing and was assigned null).
Common situations: Editor tools driven by selection where nothing is selected. Arrays built via LINQ that can be null instead of empty. Refactors that removed the array population step.
Related errors
- targetPrefab
- The path is null
- gameObjects array is empty
- Target Prefab has to be a Prefab Asset
- GameObject in input 'gameObjects' array is null
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/2e168fa68a1db5a8.
Report an issue: GitHub.