Unity-Technologies/UnityCsReference · error · ArgumentNullException

Cannot add dependency on invalid ArtifactKey.

Error message

Cannot add dependency on invalid ArtifactKey.

What it means

AssetImportContext.DependsOnArtifact(ArtifactKey key) registers a dependency on the import artifact identified by an ArtifactKey. It throws ArgumentNullException with message 'Cannot add dependency on invalid ArtifactKey.' when key.isValid is false. ArtifactKey wraps a GUID and import settings hash; an invalid key means the GUID is empty or the hash is unset, so no artifact can be resolved.

Source

Thrown at Editor/Mono/AssetPipeline/AssetImportContext.bindings.cs:152

        public byte[] GetArtifactData(ArtifactKey key, string fileName)
        {
            return GetArtifactData_ArtifactKey(key, fileName);
        }

        [Obsolete("GetOutputArtifactFilePath has been deprecated. Use SetOutputArtifactData to write artifact data from memory, or SetOutputArtifactFile to copy artifact data from a file instead.")]
        [NativeName("GetOutputArtifactFilePath_Binding")]
        public extern string GetOutputArtifactFilePath(string fileName);

        [NativeName("DependsOnImportedAsset")]
        private extern void DependsOnImportedAssetInternal(string path);

        public extern Object GetReferenceToAssetMainObject(string path);

        public void DependsOnArtifact(ArtifactKey key)
        {
            if (!key.isValid)
            {
                throw new ArgumentNullException("key", "Cannot add dependency on invalid ArtifactKey.");
            }

            DependsOnArtifactInternal(key);
        }

        [NativeName("DependsOnArtifact")]
        private extern void DependsOnArtifactInternal(ArtifactKey key);

        public void DependsOnArtifact(GUID guid)
        {
            if (guid.Empty())
            {
                throw new ArgumentNullException("guid", "Cannot add artifact dependency on empty GUID.");
            }

            DependsOnArtifactInternalGUID(guid);
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check key.isValid before calling: if (key.isValid) context.DependsOnArtifact(key).
  2. Construct ArtifactKey only from validated, non-empty GUIDs and correct import settings.
  3. Fall back to DependsOnArtifact(GUID) or DependsOnArtifact(string path) if you only have partial key data.

Example fix

// before
context.DependsOnArtifact(artifactKey);

// after
if (artifactKey.isValid)
    context.DependsOnArtifact(artifactKey);
else
    Debug.LogWarning($"{context.assetPath}: invalid ArtifactKey, skipping artifact dependency");
Defensive patterns

Strategy: validation

Validate before calling

if (key.isValid)
    context.DependsOnArtifact(key);
else
    Debug.LogWarning($"{context.assetPath}: invalid ArtifactKey");

Type guard

static bool IsValidArtifactKey(ArtifactKey key) => key.isValid;

Try / catch

try { context.DependsOnArtifact(key); }
catch (ArgumentNullException ex) when (ex.ParamName == "key")
{ Debug.LogWarning($"Skipped artifact dependency: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling context.DependsOnArtifact(key) where key was constructed from an empty GUID, or where the ArtifactKey was default-initialized (all fields zero). This overload is distinct from the GUID overload (error 29) and the string path overload (error 30).

Common situations: Advanced ScriptedImporters or internal editor tools that track artifact-level dependencies (e.g., depending on a shader's compiled variant output). The ArtifactKey can be invalid if it was built from a stale or unresolved asset reference.

Related errors


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