{"record":{"id":"e4b912439688768b","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-apply-added-gameobjects-gameobjects-array","errorCode":null,"errorMessage":"Cannot apply added GameObjects. GameObjects array is null.","messagePattern":"Cannot apply added GameObjects\\. GameObjects array is null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1647,"sourceCode":"                action,\n                startTime,\n                false\n            );\n\n            PrefabUtility.Internal_CallPrefabInstanceApplied(prefabInstanceRoot);\n        }\n\n        public static void ApplyAddedGameObject(GameObject gameObject, string assetPath, InteractionMode action)\n        {\n            ApplyAddedGameObjects(new GameObject[] { gameObject}, assetPath, action);\n        }\n\n        public static void ApplyAddedGameObjects(GameObject[] gameObjects, string assetPath, InteractionMode action)\n        {\n            DateTime startTime = DateTime.UtcNow;\n\n            if (gameObjects == null)\n                throw new ArgumentNullException(nameof(gameObjects), \"Cannot apply added GameObjects. GameObjects array is null.\");\n\n            if (gameObjects.Length == 0)\n                throw new ArgumentException(nameof(gameObjects), \"No GameObjects in array.\");\n\n            foreach (GameObject go in gameObjects)\n            {\n                if (go == null)\n                    throw new ArgumentException(nameof(go), \"Input GameObject is null.\");\n\n                if (!IsAddedGameObjectOverride(go))\n                    throw new ArgumentException(nameof(go), $\"Cannot apply added GameObject. GameObject '{go.name}' is not an added GameObject override on a Prefab instance.\");\n\n                ThrowExceptionIfInstanceIsPersistent(go);\n            }\n\n            if (gameObjects.Length > 1 && !HasSameParent(gameObjects))\n                throw new ArgumentException(nameof(gameObjects), \"ApplyAddedGameObjects requires that GameObjects share the same parent.\");\n","sourceCodeStart":1629,"sourceCodeEnd":1665,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1629-L1665","documentation":"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.","triggerScenarios":"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.","commonSituations":"Querying GetComponentsInChildren with a cast that yields null; forgetting to initialize an array field; chained calls where an intermediate step produced null.","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>())."],"exampleFix":"// before\nPrefabUtility.ApplyAddedGameObjects(null, path, InteractionMode.UserAction);\n\n// after\nvar gos = selection ?? Array.Empty<GameObject>();\nif (gos.Length > 0)\n    PrefabUtility.ApplyAddedGameObjects(gos, path, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"if (gameObjects == null) return;\n// or coalesce: gameObjects = gameObjects ?? Array.Empty<GameObject>();","typeGuard":"static bool IsNonEmptyArray(GameObject[] gos) => gos != null && gos.Length > 0;","tryCatchPattern":null,"preventionTips":["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."],"tags":["prefab","unity","null-argument","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}