Unity-Technologies/UnityCsReference · error · ArgumentException

The Prefab you want to instantiate is null.

Error message

The Prefab you want to instantiate is null.

What it means

InstantiateForAnimatorPreview requires a non-null prefab/prefab-asset; passing null throws ArgumentException immediately because the subsequent InstantiateRemoveAllNonAnimationComponents call and component enumeration would NRE. This is the standard guard used when Unity needs to build a stripped-down Animator preview object from a prefab.

Source

Thrown at Editor/Mono/EditorUtility.cs:680

                go.TryGetComponent<Renderer>(out var renderer))
            {
                // Case 1297670: Force the ambient probe for object preview in SRP.
                renderer.lightProbeUsage = LightProbeUsage.Off;
            }
        }

        internal static void InitInstantiatedPreviewRecursive(GameObject go)
        {
            go.hideFlags = HideFlags.HideAndDontSave;
            ConfigurePreviewObjectSRP(go);
            foreach (Transform c in go.transform)
                InitInstantiatedPreviewRecursive(c.gameObject);
        }

        internal static GameObject InstantiateForAnimatorPreview(Object original)
        {
            if (original == null)
                throw new ArgumentException("The Prefab you want to instantiate is null.");

            GameObject go = InstantiateRemoveAllNonAnimationComponents(original, Vector3.zero, Quaternion.identity) as GameObject;
            go.name = go.name + "AnimatorPreview";       //To avoid FindGameObject picking up our dummy object
            go.tag = "Untagged";
            InitInstantiatedPreviewRecursive(go);

            Animator[] animators = go.GetComponentsInChildren<Animator>();
            for (int i = 0; i < animators.Length; i++)
            {
                Animator animator = animators[i];

                animator.enabled = false;
                animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
                animator.logWarnings = false;
                animator.fireEvents = false;
            }

            // We absolutely need an animator on the gameObject

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check the argument before calling InstantiateForAnimatorPreview and skip the preview when it is null.
  2. If the source is an ObjectField, also test the resolved object with 'if (target == null)' rather than 'is null', since destroyed UnityEngine.Object instances are != null only via the overloaded operator.
  3. Re-resolve the prefab by GUID/asset path if it may have been reimported.

Example fix

// before
var preview = EditorUtility.InstantiateForAnimatorPreview(target);
// after
if (target != null)
    var preview = EditorUtility.InstantiateForAnimatorPreview(target);
else
    return null;
Defensive patterns

Strategy: validation

Validate before calling

if (original == null) // UnityEngine.Object overload: true for destroyed/missing
    return null;
GameObject preview = EditorUtility.InstantiateForAnimatorPreview(original);

Type guard

static bool IsUsablePrefab(UnityEngine.Object o) => o != null && o is GameObject;

Prevention

When it happens

Trigger: Calling InstantiateForAnimatorPreview(null), or passing an object reference whose Unity asset was destroyed/unloaded between assignment and call. Editor code that previews an Animator from a selection whose prefab was deleted from disk.

Common situations: A custom inspector or preview window that grabs a target via EditorGUILayout.ObjectField and the user clears the field (yielding null) before the preview redraws. Loading a scene whose referenced prefab is missing (red meta/missing reference) and the preview hook fires.

Related errors


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