CoplayDev/unity-mcp · error · Exception

Could not retrieve guid for object {obj}

Error message

Could not retrieve guid for object {obj}

What it means

Thrown by TypePreviewGeneratorBase.ObjectToMetadata when AssetDatabase.TryGetGUIDAndLocalFileIdentifier fails to resolve a GUID for a given UnityEngine.Object. The GUID is needed to construct PreviewMetadata. Failure indicates the object is not a persistent asset tracked by the AssetDatabase — it may be a scene instance, a runtime-created object, or an object whose .meta file is missing.

Source

Thrown at TestProjects/AssetStoreUploads/Packages/com.unity.asset-store-tools/Editor/Previews/Scripts/Generators/Custom/TypeGenerators/TypePreviewGeneratorBase.cs:80

            {
                if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out long _))
                    continue;

                if (Settings.IgnoredGuids.Any(x => x == guid))
                    continue;

                filteredAssets.Add(asset);
            }

            return filteredAssets;
        }

        protected abstract Task<List<PreviewMetadata>> GenerateImpl(IEnumerable<UnityEngine.Object> assets);

        protected PreviewMetadata ObjectToMetadata(UnityEngine.Object obj, string previewPath)
        {
            if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long _))
                throw new Exception($"Could not retrieve guid for object {obj}");

            return new PreviewMetadata()
            {
                Type = GenerationType.Custom,
                Guid = guid,
                Name = obj.name,
                Path = previewPath
            };
        }

        protected string GenerateOutputPathWithoutExtension(UnityEngine.Object asset, FileNameFormat fileNameFormat)
        {
            PrepareOutputFolder(Settings.OutputPath, false);
            var directoryPath = Settings.OutputPath;
            var fileName = PreviewConvertUtility.ConvertFilename(asset, fileNameFormat);
            var fullPath = $"{directoryPath}/{fileName}";

            return fullPath;

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Filter out non-persistent objects in CollectAssets before passing them to ObjectToMetadata.
  2. Verify AssetDatabase.Contains(obj) returns true before calling ObjectToMetadata.
  3. Run AssetDatabase.Refresh() if .meta files may be out of sync.
  4. Log the object's type and name when GUID resolution fails instead of throwing, to allow batch processing to continue.

Example fix

// before
protected PreviewMetadata ObjectToMetadata(UnityEngine.Object obj, string previewPath)
{
    if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long _))
        throw new Exception($"Could not retrieve guid for object {obj}");
    ...
}

// after
protected PreviewMetadata ObjectToMetadata(UnityEngine.Object obj, string previewPath)
{
    if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long _))
        throw new Exception($"Could not retrieve guid for object '{obj.name}' ({obj.GetType()}). Ensure it is a persistent AssetDatabase asset.");
    ...
}
Defensive patterns

Strategy: validation

Validate before calling

// Filter to persistent assets only before generating metadata
var persistentAssets = assets.Where(a => AssetDatabase.Contains(a)).ToList();
if (persistentAssets.Count < assets.Count())
    Debug.LogWarning($"Filtered out {assets.Count() - persistentAssets.Count} non-persistent objects.");

Type guard

static bool IsPersistentAsset(UnityEngine.Object obj)
    => obj != null && AssetDatabase.Contains(obj) &&
       AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out _, out _);

Try / catch

try
{
    var metadata = ObjectToMetadata(obj, previewPath);
}
catch (Exception ex) when (ex.Message.Contains("Could not retrieve guid"))
{
    ASDebug.LogWarning($"Skipping {obj.name}: not a persistent AssetDatabase asset.");
    continue;
}

Prevention

When it happens

Trigger: Calling ObjectToMetadata(obj, previewPath) where obj is not a persistent AssetDatabase asset. TryGetGUIDAndLocalFileIdentifier returns false.

Common situations: CollectAssets returned a non-persistent object (e.g., a material instance created at runtime); asset's .meta file was deleted; asset was moved and the GUID is stale; object is a sub-asset or embedded resource not directly addressable by GUID.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/c199d7f26c7b19c2. Report an issue: GitHub.