Unity-Technologies/UnityCsReference · error · ArgumentNullException

asset

Error message

asset

What it means

AssetImporter.SourceAssetIdentifier(Object asset) constructor creates an identifier from a Unity Object's type and name. It throws ArgumentNullException with param name 'asset' when the passed Object is null, because it needs asset.GetType() and asset.name to populate the struct. SourceAssetIdentifier is used to map sub-assets within an importer (e.g., remapping materials or meshes in model importers).

Source

Thrown at Editor/Mono/AssetPipeline/AssetImporter.bindings.cs:31

using UnityEditor.Experimental;

namespace UnityEditor
{
    [NativeHeader("Editor/Src/AssetPipeline/AssetImporter.h")]
    [NativeHeader("Editor/Src/AssetPipeline/AssetImporter.bindings.h")]
    [ExcludeFromObjectFactory]
    [Preserve]
    [global::UnityEngine.NativeClass("AssetImporter", PersistentTypeId = 1003)]
    [UsedByNativeCode]
    public partial class AssetImporter : Object
    {
        public struct SourceAssetIdentifier
        {
            public SourceAssetIdentifier(Object asset)
            {
                if (asset == null)
                {
                    throw new ArgumentNullException("asset");
                }

                this.type = asset.GetType();
                this.name = asset.name;
            }

            public SourceAssetIdentifier(Type type, string name)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type");
                }

                if (name == null)
                {
                    throw new ArgumentNullException("name");
                }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check before constructing: if (obj != null) new SourceAssetIdentifier(obj).
  2. Use the (Type, string) overload instead if you already know the type and name, avoiding the Object dependency.
  3. Validate the loaded asset's type matches expectations before passing to the constructor.

Example fix

// before
var id = new SourceAssetIdentifier(loadedAsset);

// after
if (loadedAsset != null)
    var id = new SourceAssetIdentifier(loadedAsset);
else
    Debug.LogWarning("Cannot create SourceAssetIdentifier: asset is null");
Defensive patterns

Strategy: validation

Validate before calling

if (asset != null)
    var id = new AssetImporter.SourceAssetIdentifier(asset);
else
    Debug.LogWarning("Cannot create SourceAssetIdentifier: null asset");

Type guard

static bool IsValidUnityObject(Object obj) => obj != null;

Try / catch

try { var id = new AssetImporter.SourceAssetIdentifier(asset); }
catch (ArgumentNullException ex) when (ex.ParamName == "asset")
{ Debug.LogWarning($"Skipped SourceAssetIdentifier: {ex.Message}"); }

Prevention

When it happens

Trigger: Constructing new SourceAssetIdentifier(obj) where obj is null. This happens when loading a sub-asset via AssetDatabase.LoadAssetAtPath returns null (asset not found, wrong type, or not yet imported) and the null is passed directly to the constructor without a null check.

Common situations: Model importer remapping code, material remap utilities, or editor scripts that build SourceAssetIdentifier lists from loaded assets. The null arises from failed asset loads, type mismatches in 'as' casts, or assets that haven't been imported yet at the time of the call.

Related errors


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