Unity-Technologies/UnityCsReference · error · ArgumentNullException

Value for parameter atlases is null

Error message

Value for parameter atlases is null

What it means

Thrown by SpriteAtlasUtility.PackAtlases when the atlases array argument is null. PackAtlases packs a caller-supplied set of SpriteAtlas assets into textures via the native PackAtlasesInternal; before delegating it asserts the array exists. ArgumentNullException is used because the whole parameter (not just an element) is missing, so there is nothing to iterate.

Solutions

  1. Initialize atlases via AssetDatabase.FindAssets("t:SpriteAtlas") and LoadAssetAtPath before calling PackAtlases.
  2. Guard with 'if (atlases == null || atlases.Length == 0) return;' for optional-pack workflows.
  3. Log a clear message and skip when the selection/gather step yields null instead of forwarding it.
  4. Verify the array is populated in the same scope that creates it, not in a deferred callback.

Example fix

// before
SpriteAtlasUtility.PackAtlases(null, BuildTarget.StandaloneWindows64);

// after
var guids = AssetDatabase.FindAssets("t:SpriteAtlas");
var atlases = guids.Select(g => AssetDatabase.LoadAssetAtPath<SpriteAtlas>(AssetDatabase.GUIDToAssetPath(g))).ToArray();
if (atlases == null || atlases.Length == 0) { Debug.LogWarning("No sprite atlases found; skipping pack."); return; }
SpriteAtlasUtility.PackAtlases(atlases, BuildTarget.StandaloneWindows64);
Defensive patterns

Strategy: validation

Validate before calling

if (atlases == null) { Debug.LogWarning("atlases is null; nothing to pack."); return; }

Type guard

static bool HasAtlases(SpriteAtlas[] a) => a != null && a.Length > 0;

Prevention

When it happens

Trigger: Calling SpriteAtlasUtility.PackAtlases(null, target). Passing a field/property that was never assigned (e.g. a SerializedProperty.objectReferenceValue-derived array that came back null). Forwarding the result of FindObjectsByType or AssetDatabase.FindAssets that returned nothing and got coerced to null.

Common situations: Editor build scripts and CI batch packing pipelines that gather atlases dynamically. PostImport or BuildPlayer callbacks that pack atlases where asset loading failed silently. Menu-item automation where the user forgot to select atlases in the Project window.

Related errors


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

Appendix: source

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

using UnityEditor.AssetImporters;
using UnityEngine.UIElements;

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)]

View on GitHub (pinned to 225b0fbdb5)