Unity-Technologies/UnityCsReference · error · ArgumentNullException

GameObject is null.

Error message

GameObject is null.

What it means

PrefabUtility.IsAddedGameObjectOverride throws an ArgumentNullException when gameObject is null. This method checks whether a GameObject represents an added override within a prefab instance and requires a valid GameObject to inspect its transform hierarchy.

Source

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

        // Called after prefab instances in the scene have been updated
        public delegate void PrefabInstanceUpdated(GameObject instance);
        [AutoStaticsCleanupOnCodeReload]
        public static PrefabInstanceUpdated prefabInstanceUpdated;
        [AutoStaticsCleanupOnCodeReload(CleanupStrategy = CleanupStrategy.Clear)]
        private static DelegateWithPerformanceTracker<PrefabInstanceUpdated> m_PrefabInstanceUpdated = new DelegateWithPerformanceTracker<PrefabInstanceUpdated>($"{nameof(PrefabUtility)}.{nameof(prefabInstanceUpdated)}");

        [RequiredByNativeCode]
        private static void Internal_CallPrefabInstanceUpdated(GameObject instance)
        {
            foreach (var evt in m_PrefabInstanceUpdated.UpdateAndInvoke(prefabInstanceUpdated))
                evt(instance);
        }

        public static bool IsAddedGameObjectOverride(GameObject gameObject)
        {
            if (gameObject == null)
                throw new ArgumentNullException(nameof(gameObject), "GameObject is null.");

            Transform parent = gameObject.transform.parent;
            if (parent == null)
                return false;

            // Can't be added to a prefab instance if the parent is not part of a prefab instance.
            GameObject parentAsset = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(parent.gameObject);
            if (parentAsset == null)
                return false;

            GameObject asset = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(gameObject);

            // If object is not part of a prefab (but the parent is) we know it's added.
            if (asset == null)
                return true;

            // We know now that the object is part of a prefab.
            // If the root of that prefab, then it can't be part of the parent prefab, and must be added.

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Add a null check before calling: if (gameObject != null) before IsAddedGameObjectOverride.
  2. Use Unity's null comparison pattern (gameObject != null handles destroyed objects).
  3. When iterating child collections, validate each element before passing.

Example fix

// before
bool added = PrefabUtility.IsAddedGameObjectOverride(maybeDestroyedGo);
// after
if (gameObject != null)
    bool added = PrefabUtility.IsAddedGameObjectOverride(gameObject);
Defensive patterns

Strategy: validation

Validate before calling

if (gameObject != null)
    bool isAdded = PrefabUtility.IsAddedGameObjectOverride(gameObject);

Type guard

static bool IsValidGameObject(GameObject go) => go != null; // Unity null check handles destroyed objects

Prevention

When it happens

Trigger: Calling PrefabUtility.IsAddedGameObjectOverride(null) or passing a destroyed/missing GameObject reference.

Common situations: Calling this method inside a loop over children where one child was destroyed during iteration, or checking a GameObject field that was never assigned. Also common with Unity's fake-null pattern where a destroyed object appears non-null in C# but fails the Unity null check.

Related errors


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