{"record":{"id":"a98ee3c521f49c82","repo":"Unity-Technologies/UnityCsReference","slug":"plaingameobjects","errorCode":null,"errorMessage":"plainGameObjects","messagePattern":"plainGameObjects","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2391,"sourceCode":"                var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(plainGameObject);\n                if (gameObjectsWithInvalidScript.Count > 0)\n                    throw new InvalidOperationException(string.Format($\"Cannot convert the GameObject when it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' has a missing script. This is not supported by the Undo system. Use InteractionMode.AutomatedAction instead.\"));\n            }\n        }\n\n        public static void ConvertToPrefabInstance(GameObject plainGameObject, GameObject prefabAssetRoot, ConvertToPrefabInstanceSettings settings, InteractionMode mode)\n        {\n            ThrowIfInvalidArgumentsForConvertToPrefabInstance(plainGameObject, prefabAssetRoot, true, mode);\n\n            ConvertToPrefabInstance_NoInputValidation(plainGameObject, prefabAssetRoot, settings, mode);\n\n            EditorUtility.ForceRebuildInspectors();\n        }\n\n        public static void ConvertToPrefabInstances(GameObject[] plainGameObjects, GameObject prefabAssetRoot, ConvertToPrefabInstanceSettings settings, InteractionMode mode)\n        {\n            if (plainGameObjects == null)\n                throw new ArgumentNullException(nameof(plainGameObjects));\n\n            if (plainGameObjects.Length == 0)\n                throw new ArgumentException(nameof(plainGameObjects) + \" has no objects\");\n\n            foreach(var go in plainGameObjects)\n                if (go == null)\n                    throw new ArgumentException(nameof(plainGameObjects) + \" has a null GameObject\");\n\n            if (settings == null)\n                throw new ArgumentNullException(nameof(settings));\n\n            ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);\n\n            var topLevelGameObjects = GetTopLevelGameObjects(plainGameObjects);\n            foreach (var go in topLevelGameObjects)\n                ThrowIfInvalidArgumentsForConvertToPrefabInstance(go, prefabAssetRoot, false, mode);\n\n            foreach (var go in topLevelGameObjects)","sourceCodeStart":2373,"sourceCodeEnd":2409,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2373-L2409","documentation":"PrefabUtility.ConvertToPrefabInstances throws an ArgumentNullException when the plainGameObjects array argument is null. This API converts an array of regular scene GameObjects into prefab instances and requires a non-null array to iterate over.","triggerScenarios":"Calling PrefabUtility.ConvertToPrefabInstances(null, prefabAssetRoot, settings, mode) where the first argument is null rather than an array of GameObjects.","commonSituations":"Passing an uninitialized array, a field that was never assigned, or the result of a LINQ/builder call that returned null instead of an empty array. Also happens when deserializing a list that failed to populate.","solutions":["Pass a non-null GameObject[] (use new GameObject[0] or Array.Empty<GameObject>() if empty).","If building the array dynamically, initialize it before the call: var gos = new List<GameObject>() and then gos.ToArray().","If the array comes from Selection.gameObjects or FindObjectsOfType, guard with a null check before calling."],"exampleFix":"// before\nPrefabUtility.ConvertToPrefabInstances(null, root, settings, InteractionMode.AutomatedAction);\n// after\nvar gos = Selection.gameObjects ?? Array.Empty<GameObject>();\nif (gos.Length > 0)\n    PrefabUtility.ConvertToPrefabInstances(gos, root, settings, InteractionMode.AutomatedAction);","handlingStrategy":"validation","validationCode":"if (plainGameObjects == null) return; // or initialize\nvar safeArray = plainGameObjects ?? Array.Empty<GameObject>();\nif (safeArray.Length == 0) return;\nPrefabUtility.ConvertToPrefabInstances(safeArray, root, settings, mode);","typeGuard":"static bool IsValidGameObjectArray(GameObject[] arr) => arr != null && arr.Length > 0 && arr.All(g => g != null);","tryCatchPattern":null,"preventionTips":["Initialize arrays before passing to Unity APIs","Use Selection.gameObjects ?? Array.Empty<GameObject>() for safe defaults","Validate collection arguments in your own wrapper methods"],"tags":["unity","prefab","argument-null","editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}