Unity-Technologies/UnityCsReference · error · ArgumentNullException

gameObject

Error message

gameObject

What it means

Thrown by AnimationUtility.GetAnimationClips(GameObject) when gameObject is null. The method immediately calls GetAnimationClipsInAnimationPlayer(gameObject) and GetComponents<IAnimationClipSource>, both of which would dereference null; the guard fails fast with ArgumentNullException instead of an opaque NullReferenceException from native code.

Source

Thrown at Editor/Mono/Animation/AnimationUtility.bindings.cs:126

        [RequiredByNativeCode]
        private static void Internal_CallAnimationClipAwake(AnimationClip clip)
        {
            if (onCurveWasModified != null)
                onCurveWasModified(clip, new EditorCurveBinding(), CurveModifiedType.ClipModified);
        }

        [Obsolete("GetAnimationClips(Animation) is deprecated. Use GetAnimationClips(GameObject) instead.")]
        public static AnimationClip[] GetAnimationClips(Animation component)
        {
            return GetAnimationClipsInAnimationPlayer(component.gameObject);
        }

        // Returns the array of AnimationClips that are referenced in the Animation component
        public static AnimationClip[] GetAnimationClips(GameObject gameObject)
        {
            if (gameObject == null)
                throw new ArgumentNullException("gameObject");

            AnimationClip[] clips = GetAnimationClipsInAnimationPlayer(gameObject);

            IAnimationClipSource[] clipSources = gameObject.GetComponents<IAnimationClipSource>();
            if (clipSources.Length > 0)
            {
                var allClips = new List<AnimationClip>(clips);
                for (int i = 0; i < clipSources.Length; ++i)
                {
                    var extraClips = new List<AnimationClip>();
                    clipSources[i].GetAnimationClips(extraClips);

                    allClips.Capacity = allClips.Count + extraClips.Count;
                    foreach (var clip in extraClips)
                    {
                        if (clip != null)
                            allClips.Add(clip);
                    }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check with UnityEngine.Object's overloaded == before calling: if (go == null) return;.
  2. Cache validated references and re-check after any yield/EditorApplication.delayCall.
  3. Use TryGetComponent or GetComponentsInChildren without destroyed objects by refreshing the query.
  4. Guard against the destroyed-but-not-C#-null case explicitly: if (!go) return;.

Example fix

// before
var clips = AnimationUtility.GetAnimationClips(target);

// after
if (target == null) { Debug.LogWarning("GameObject is null/destroyed; skipping."); return Array.Empty<AnimationClip>(); }
var clips = AnimationUtility.GetAnimationClips(target);
Defensive patterns

Strategy: type-guard

Validate before calling

if (gameObject == null || !gameObject) { Debug.LogWarning("GameObject null or destroyed."); return Array.Empty<AnimationClip>(); }

Type guard

static bool IsLive(UnityEngine.Object o) => o != null;

Prevention

When it happens

Trigger: Calling GetAnimationClips(gameObject) where gameObject was destroyed between capture and use. Passing transform.gameObject from a destroyed Transform. A DestroyImmediate in the same frame leaves a 'not-null but destroyed' UnityEngine.Object that fails the == null override.

Common situations: Editor scripts scanning hierarchy during scene close. Prefab processing where the GameObject was unloaded. Timeline/Animator tooling iterating a filtered list that retained destroyed references.

Related errors


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