{"record":{"id":"8e229d8ea06b9b5d","repo":"Unity-Technologies/UnityCsReference","slug":"gameobjects-array-is-empty","errorCode":null,"errorMessage":"gameObjects array is empty","messagePattern":"gameObjects array is empty","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":277,"sourceCode":"        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SaveAsPrefabAsset_Internal([NotNull] GameObject root, string path, out bool success);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SaveAsPrefabAssetAndConnect_Internal([NotNull] GameObject root, string path, out bool success);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SavePrefabAsset_Internal([NotNull] GameObject root, out bool success);\n\n        internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab)\n        {\n            if (gameObjects == null)\n                throw new ArgumentNullException(\"gameObjects\");\n\n            if (gameObjects.Length == 0)\n                throw new ArgumentException(\"gameObjects array is empty\");\n\n            if (targetPrefab == null)\n                throw new ArgumentNullException(\"targetPrefab\");\n\n            if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab))\n                throw new ArgumentException(\"Target Prefab has to be a Prefab Asset\");\n\n            Object targetPrefabInstance = null;\n\n            var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);\n\n            foreach (GameObject go in gameObjects)\n            {\n                if (go == null)\n                    throw new ArgumentException(\"GameObject in input 'gameObjects' array is null\");\n\n                if (EditorUtility.IsPersistent(go))  // Prefab asset\n                    throw new ArgumentException(\"Game object is part of a prefab\");","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L259-L295","documentation":"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.","triggerScenarios":"Calling with new GameObject[0], Array.Empty<GameObject>(), or a filtered/selected list that resolved to no elements.","commonSituations":"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.","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."],"exampleFix":"// before\nvar gos = Selection.gameObjects.Where(g => g.CompareTag(\"Enemy\")).ToArray();\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // may be empty\n\n// after\nvar gos = Selection.gameObjects.Where(g => g.CompareTag(\"Enemy\")).ToArray();\nif (gos.Length == 0) { Debug.LogWarning(\"No Enemy selected.\"); return; }\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"if (gameObjects == null || gameObjects.Length == 0)\n{ Debug.LogWarning(\"No GameObjects to connect.\"); return; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["prefab","add-to-prefab","empty-argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}