Unity-Technologies/UnityCsReference · error · ArgumentNullException

One of the elements in atlases is null. Please check your In

Error message

One of the elements in atlases is null. Please check your Inputs.

What it means

Thrown by SpriteAtlasUtility.PackAtlases during its per-element loop when at least one slot in the atlases array is null. Unlike the whole-array check, the array itself is valid but contains a missing reference, which the native packer cannot dereference. The message explicitly tells the developer to inspect the Inputs, i.e. the objects feeding the array.

Source

Thrown at Editor/Mono/2D/SpriteAtlas/EditorSpriteAtlas.bindings.cs:31

namespace UnityEditor.U2D
{
    [NativeHeader("Runtime/2D/SpriteAtlas/SpriteAtlas.h")]
    [NativeHeader("Editor/Src/2D/SpriteAtlas/SpriteAtlasPackingUtilities.h")]
    public class SpriteAtlasUtility
    {
        [FreeFunction("CollectAllSpriteAtlasesAndPack")]
        extern public static void PackAllAtlases(BuildTarget target, bool canCancel = true);

        [FreeFunction("PackSpriteAtlases")]
        extern internal static void PackAtlasesInternal(SpriteAtlas[] atlases, BuildTarget target, bool canCancel = true, bool invokedFromImporter = false, bool unloadSprites = false);

        public static void PackAtlases(SpriteAtlas[] atlases, BuildTarget target, bool canCancel = true)
        {
            if (atlases == null)
                throw new ArgumentNullException("atlases", "Value for parameter atlases is null");
            foreach (var atlas in atlases)
                if (atlas == null)
                    throw new ArgumentNullException("atlases", "One of the elements in atlases is null. Please check your Inputs.");
            PackAtlasesInternal(atlases, target, canCancel, false, true);
        }

        [FreeFunction("GetSpriteTexture")]
        extern internal static Texture2D GetSpriteTexture([NotNull] Sprite sprite, bool fromAtlas);

        [FreeFunction("SpriteAtlasExtensions::CleanupAtlasPacking")]
        extern public static void CleanupAtlasPacking();

        [FreeFunction("SpriteAtlasExtensions::OnSpriteAtlasSettingsChanged")]
        extern internal static void OnSpriteAtlasSettingsChanged();
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct SpriteAtlasTextureSettings
    {
        [NativeName("anisoLevel")]

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter nulls out before packing: atlases = atlases.Where(a => a != null).ToArray().
  2. Validate each loaded asset immediately after LoadAssetAtPath and skip/log failures.
  3. Use EditorUtility.CollectDependencies or a typed selector so only real SpriteAtlas objects enter the array.
  4. If the empty slot is a user error, surface it in the editor UI before invoking PackAtlases.

Example fix

// before
var atlases = selectedPaths.Select(p => AssetDatabase.LoadAssetAtPath<SpriteAtlas>(p)).ToArray();
SpriteAtlasUtility.PackAtlases(atlases, target);

// after
var atlases = selectedPaths
    .Select(p => AssetDatabase.LoadAssetAtPath<SpriteAtlas>(p))
    .Where(a => a != null)
    .ToArray();
if (atlases.Any(a => a == null)) { Debug.LogError("Some atlases could not be loaded."); return; }
SpriteAtlasUtility.PackAtlases(atlases, target);
Defensive patterns

Strategy: validation

Validate before calling

if (atlases == null || atlases.Any(a => a == null)) { Debug.LogError("atlases contains a null element."); return; }

Type guard

static bool AllAtlasesValid(SpriteAtlas[] a) => a != null && a.All(x => x != null);

Prevention

When it happens

Trigger: An array built with a placeholder null, or LoadAssetAtPath returning null for a bad path but still added to the list. Mixing valid SpriteAtlas objects with destroyed/unloaded ones (e.g. after Resources.UnloadUnusedAssets). A selection array containing a non-SpriteAtlas that got cast and failed silently.

Common situations: Custom editor windows that let users add atlas slots but leave some empty. Batch scripts that filter by name but a moved/renamed asset leaves a null entry. Prefab/asset-bundle build automation iterating a list that contains stale references.

Related errors


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