Unity-Technologies/UnityCsReference · error · ArgumentNullException

Cannot revert added GameObject. GameObject is null.

Error message

Cannot revert added GameObject. GameObject is null.

What it means

RevertAddedGameObject requires a non-null gameObject. The method immediately inspects the object (IsAddedGameObjectOverride), so a null reference cannot be processed and ArgumentNullException is thrown.

Source

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

                throw new ArgumentException("Prefab instance should match Prefab source.");
            if (assetGameObject.transform.root == assetGameObject.transform)
                throw new ArgumentException("The asset GameObject cannot be the root as the root cannot be removed as an override.");

            var actionName = "Revert Prefab removed GameObject";
            var prefabInstanceHandle = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);

            GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);

            if (action == InteractionMode.UserAction)
                Undo.RegisterFullObjectHierarchyUndo(prefabInstanceRoot, actionName);

            RemoveRemovedGameObjectOverride(prefabInstanceHandle, assetGameObject);
        }

        public static void RevertAddedGameObject(GameObject gameObject, InteractionMode action)
        {
            if (gameObject == null)
                throw new ArgumentNullException(nameof(gameObject), "Cannot revert added GameObject. GameObject is null.");

            if (!IsAddedGameObjectOverride(gameObject))
                throw new ArgumentException("Cannot revert added GameObject. GameObject is not an added GameObject override on a Prefab instance.", nameof(gameObject));

            ThrowExceptionIfInstanceIsPersistent(gameObject);

            GameObject instance = PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject.transform.parent);
            PrefabUtility.Internal_CallPrefabInstanceReverting(instance);

            if (action == InteractionMode.UserAction)
                Undo.DestroyObjectImmediate(gameObject);
            else
                Object.DestroyImmediate(gameObject);

            PrefabUtility.Internal_CallPrefabInstanceReverted(instance);
        }

        public static List<ObjectOverride> GetObjectOverrides(GameObject prefabInstance, bool includeDefaultOverrides = false)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check gameObject before calling.
  2. Refresh the additions list immediately before reverting using GetAddedGameObjects.
  3. Skip/log null entries instead of forwarding them.

Example fix

// before
PrefabUtility.RevertAddedGameObject(go, mode);

// after
if (go != null)
    PrefabUtility.RevertAddedGameObject(go, mode);
Defensive patterns

Strategy: validation

Validate before calling

if (gameObject == null) return;

Type guard

static bool IsLiveGameObject(GameObject go) => go != null && go != null;

Prevention

When it happens

Trigger: Calling RevertAddedGameObject(null, action); passing a destroyed or uninitialized reference.

Common situations: Iterating a collection of additions where some references were destroyed between capture and revert; editor scripts operating on stale selection data.

Related errors


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