Unity-Technologies/UnityCsReference · error · ArgumentException
gameObjects array is empty
Error message
gameObjects array is empty
What it means
Thrown by AddGameObjectsToPrefabAndConnect when the `gameObjects` array is non-null but has zero elements. Connecting nothing is meaningless, so Unity rejects it as ArgumentException before touching the target prefab.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.bindings.cs:277
[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");
if (EditorUtility.IsPersistent(go)) // Prefab asset
throw new ArgumentException("Game object is part of a prefab");View on GitHub (pinned to 225b0fbdb5)
Solutions
- Check Length > 0 before calling and early-return or warn the user.
- Filter/guarantee at least one valid GameObject upstream.
- Disable the menu item / button when the selection is empty.
Example fix
// before
var gos = Selection.gameObjects.Where(g => g.CompareTag("Enemy")).ToArray();
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // may be empty
// after
var gos = Selection.gameObjects.Where(g => g.CompareTag("Enemy")).ToArray();
if (gos.Length == 0) { Debug.LogWarning("No Enemy selected."); return; }
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); Defensive patterns
Strategy: validation
Validate before calling
if (gameObjects == null || gameObjects.Length == 0)
{ Debug.LogWarning("No GameObjects to connect."); return; } Prevention
- Check Length > 0 before calling and warn the user when selection is empty.
- Disable menu items / buttons when the selection is empty via Validate functions.
- Filter selections to guarantee at least one valid element upstream.
When it happens
Trigger: Calling with new GameObject[0], Array.Empty<GameObject>(), or a filtered/selected list that resolved to no elements.
Common situations: Selection-based tools when the user invoked the action with nothing selected. LINQ Where clauses filtering out everything. Loops that never Add to a list.
Related errors
- gameObjects
- targetPrefab
- Target Prefab has to be a Prefab Asset
- GameObject in input 'gameObjects' array is null
- Game object is part of a prefab
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/8e229d8ea06b9b5d.
Report an issue: GitHub.