Unity-Technologies/UnityCsReference · error · NullReferenceException

Cannot apply or revert object. Object is null.

Error message

Cannot apply or revert object. Object is null.

What it means

Declared in ThrowExceptionIfNotValidPrefabInstanceObject as a NullReferenceException for a null prefabInstanceObject. NOTE: because the type-check (`is GameObject/Component`) runs first and `null is X` is always false, a null input actually triggers error 555 first, so this specific NullReferenceException is effectively unreachable through this guard in normal flow. It documents intent: apply/revert must not receive null.

Source

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

                    {
                        assetsToReload.Add(importer.assetPath);
                    }
                }
            }

            foreach (var path in assetsToReload)
            {
                AssetDatabase.WriteImportSettingsIfDirty(path);
                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
            }
        }

        static void ThrowExceptionIfNotValidPrefabInstanceObject(Object prefabInstanceObject, bool isApply)
        {
            if (!(prefabInstanceObject is GameObject || prefabInstanceObject is Component))
                throw new ArgumentException("Calling apply or revert methods on an object which is not a GameObject or Component is not supported.", nameof(prefabInstanceObject));
            if (prefabInstanceObject == null)
                throw new NullReferenceException("Cannot apply or revert object. Object is null.");
            if (!PrefabUtility.IsPartOfPrefabInstance(prefabInstanceObject))
                throw new ArgumentException("Calling apply or revert methods on an object which is not part of a Prefab instance is not supported.", nameof(prefabInstanceObject));

            // We support revert operations on Prefab Assets, but not apply operations.
            if (isApply)
                ThrowExceptionIfInstanceIsPersistent(prefabInstanceObject);
        }

        static void ThrowExceptionIfAllPrefabInstanceObjectsAreInvalid(Object[] prefabInstanceObjects, bool isApply)
        {
            foreach (var obj in prefabInstanceObjects)
            {
                if (obj != null && (obj is GameObject || obj is Component) && IsPartOfPrefabInstance(obj) && !(isApply && EditorUtility.IsPersistent(obj)))
                    return;
            }

            // Throw exception if all objects are invalid
            throw new ArgumentException("Cannot apply or revert on any of the objects. Attempt with individual objects for details.", nameof(prefabInstanceObjects));

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the object is non-null before calling apply/revert (this also prevents 555).
  2. Treat any null-input apply/revert failure as error 555 and fix the type/null upstream.
  3. Add an explicit null guard upstream so you get your own clear message, not Unity's.

Example fix

// before
PrefabUtility.ApplyPrefabOverride(null, path); // lands as 555 in practice

// after
if (obj == null) { Debug.LogWarning("Object is null; cannot apply."); return; }
if (!(obj is GameObject || obj is Component)) return;
PrefabUtility.ApplyPrefabOverride(obj, path);
Defensive patterns

Strategy: validation

Validate before calling

if (prefabInstanceObject == null)
{ Debug.LogWarning("Cannot apply/revert a null object."); return; }

Prevention

When it happens

Trigger: Theoretically a null prefabInstanceObject; in practice unreachable because the preceding `is` guard rejects null as 'not a GameObject or Component'. Could surface only if the method ordering changed or a subclass made `is` return true for null (which C# does not).

Common situations: Effectively never hit given the current ordering; treat a null-related apply/revert failure as error 555. Included for completeness/defense.

Related errors


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