{"record":{"id":"d0999de1daae5360","repo":"Unity-Technologies/UnityCsReference","slug":"input-gameobject-is-null","errorCode":null,"errorMessage":"Input GameObject is null.","messagePattern":"Input GameObject is null\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1655,"sourceCode":"        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\n            GameObject gameObject = gameObjects[0];\n            Transform instanceParent = gameObject.transform.parent;\n            if (instanceParent == null)\n                return;\n\n            GameObject prefabSourceGameObjectParent = GetCorrespondingObjectFromSourceAtPath(instanceParent.gameObject, assetPath);\n            if (prefabSourceGameObjectParent == null)\n                return;","sourceCodeStart":1637,"sourceCodeEnd":1673,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L1637-L1673","documentation":"Inside the per-element loop of ApplyAddedGameObjects, a null entry in the array is rejected with ArgumentException. Each element must be a live GameObject reference because the method inspects IsAddedGameObjectOverride on each one.","triggerScenarios":"Array contains a null element due to a destroyed object, an uninitialized slot, or a failed lookup that slipped into the collection.","commonSituations":"Collecting GameObjects where some were destroyed between collection and the apply call; arrays built with a fixed size but only partially filled; references cleared by another system mid-frame.","solutions":["Filter nulls before calling: gameObjects = gameObjects.Where(g => g != null).ToArray();","Avoid storing destroyed references; rebuild arrays from current selection right before apply.","Prefer List<GameObject> populated explicitly over fixed-size arrays with leftover nulls."],"exampleFix":"// before\nPrefabUtility.ApplyAddedGameObjects(gos, path, mode);\n\n// after\ngos = gos.Where(g => g != null).ToArray();\nif (gos.Length > 0)\n    PrefabUtility.ApplyAddedGameObjects(gos, path, mode);","handlingStrategy":"validation","validationCode":"gameObjects = gameObjects?.Where(g => g != null).ToArray();\nif (gameObjects == null || gameObjects.Length == 0) return;","typeGuard":"static bool AllNonNull(GameObject[] gos) => gos != null && gos.All(g => g != null);","tryCatchPattern":null,"preventionTips":["Rebuild arrays from current selection immediately before applying to avoid destroyed references.","Filter nulls out of any collection derived from GetComponentsInChildren or selection APIs.","Avoid fixed-size arrays partially filled with nulls."],"tags":["prefab","unity","null-element","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}