Unity-Technologies/UnityCsReference · error · ArgumentException

plainGameObjects has a null GameObject

Error message

plainGameObjects has a null GameObject

What it means

PrefabUtility.ConvertToPrefabInstances throws an ArgumentException when any element in the plainGameObjects array is null. Each GameObject must be a valid reference because the API iterates and converts each one individually.

Source

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

        {
            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)
                ConvertToPrefabInstance_NoInputValidation(go, prefabAssetRoot, settings, mode);

            EditorUtility.ForceRebuildInspectors();
        }

        private static void ConvertToPrefabInstance_NoInputValidation(GameObject plainGameObject, GameObject prefabAssetRoot, ConvertToPrefabInstanceSettings settings, InteractionMode mode)
        {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter out null elements before calling: gos = gos.Where(g => g != null && g != null).ToArray() (checking both C# null and Unity's fake-null).
  2. Use gos.Where(g => g != null).ToArray() after checking Unity's null comparison for destroyed objects.
  3. Rebuild the array from a fresh source (Selection, FindObjectsOfType) right before the call.

Example fix

// before
var gos = new GameObject[] { obj1, null, obj3 };
PrefabUtility.ConvertToPrefabInstances(gos, root, settings, mode);
// after
var gos = new[] { obj1, obj3 }.Where(g => g != null).ToArray();
if (gos.Length > 0)
    PrefabUtility.ConvertToPrefabInstances(gos, root, settings, mode);
Defensive patterns

Strategy: validation

Validate before calling

var valid = plainGameObjects.Where(g => g != null).ToArray();
if (valid.Length > 0)
    PrefabUtility.ConvertToPrefabInstances(valid, root, settings, mode);

Type guard

static GameObject[] FilterValid(GameObject[] arr) => arr?.Where(g => g != null).ToArray() ?? Array.Empty<GameObject>();

Prevention

When it happens

Trigger: Calling ConvertToPrefabInstances with an array containing at least one null element (e.g., a destroyed GameObject reference, or an array slot that was never assigned).

Common situations: GameObject was destroyed between array population and the API call, a FindObjectsOfType result included stale references, or a manually constructed array has uninitialized slots.

Related errors


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