Unity-Technologies/UnityCsReference · error · ArgumentNullException

plainGameObjects

Error message

plainGameObjects

What it means

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.

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2391

                var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(plainGameObject);
                if (gameObjectsWithInvalidScript.Count > 0)
                    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."));
            }
        }

        public static void ConvertToPrefabInstance(GameObject plainGameObject, GameObject prefabAssetRoot, ConvertToPrefabInstanceSettings settings, InteractionMode mode)
        {
            ThrowIfInvalidArgumentsForConvertToPrefabInstance(plainGameObject, prefabAssetRoot, true, mode);

            ConvertToPrefabInstance_NoInputValidation(plainGameObject, prefabAssetRoot, settings, mode);

            EditorUtility.ForceRebuildInspectors();
        }

        public static void ConvertToPrefabInstances(GameObject[] plainGameObjects, GameObject prefabAssetRoot, ConvertToPrefabInstanceSettings settings, InteractionMode mode)
        {
            if (plainGameObjects == null)
                throw new ArgumentNullException(nameof(plainGameObjects));

            if (plainGameObjects.Length == 0)
                throw new ArgumentException(nameof(plainGameObjects) + " has no objects");

            foreach(var go in plainGameObjects)
                if (go == null)
                    throw new ArgumentException(nameof(plainGameObjects) + " has a null GameObject");

            if (settings == null)
                throw new ArgumentNullException(nameof(settings));

            ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);

            var topLevelGameObjects = GetTopLevelGameObjects(plainGameObjects);
            foreach (var go in topLevelGameObjects)
                ThrowIfInvalidArgumentsForConvertToPrefabInstance(go, prefabAssetRoot, false, mode);

            foreach (var go in topLevelGameObjects)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass a non-null GameObject[] (use new GameObject[0] or Array.Empty<GameObject>() if empty).
  2. If building the array dynamically, initialize it before the call: var gos = new List<GameObject>() and then gos.ToArray().
  3. If the array comes from Selection.gameObjects or FindObjectsOfType, guard with a null check before calling.

Example fix

// before
PrefabUtility.ConvertToPrefabInstances(null, root, settings, InteractionMode.AutomatedAction);
// after
var gos = Selection.gameObjects ?? Array.Empty<GameObject>();
if (gos.Length > 0)
    PrefabUtility.ConvertToPrefabInstances(gos, root, settings, InteractionMode.AutomatedAction);
Defensive patterns

Strategy: validation

Validate before calling

if (plainGameObjects == null) return; // or initialize
var safeArray = plainGameObjects ?? Array.Empty<GameObject>();
if (safeArray.Length == 0) return;
PrefabUtility.ConvertToPrefabInstances(safeArray, root, settings, mode);

Type guard

static bool IsValidGameObjectArray(GameObject[] arr) => arr != null && arr.Length > 0 && arr.All(g => g != null);

Prevention

When it happens

Trigger: Calling PrefabUtility.ConvertToPrefabInstances(null, prefabAssetRoot, settings, mode) where the first argument is null rather than an array of GameObjects.

Common situations: 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.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/a98ee3c521f49c82. Report an issue: GitHub.