Unity-Technologies/UnityCsReference · error · ArgumentNullException

Prefab Asset path is null or empty

Error message

Prefab Asset path is null or empty

What it means

PrefabUtility.LoadPrefabContents throws an ArgumentNullException when the assetPath is null or empty. This method loads a prefab asset's full contents into a temporary preview scene for editing, and requires a valid file path to the .prefab file.

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2903

            if (!PrefabUtility.IsPartOfAnyPrefab(componentOrGameObject))
                return PrefabAssetType.NotAPrefab;

            if (PrefabUtility.IsPrefabAssetMissing(componentOrGameObject))
                return PrefabAssetType.MissingAsset;

            if (PrefabUtility.IsPartOfVariantPrefab(componentOrGameObject))
                return PrefabAssetType.Variant;

            if (PrefabUtility.IsPartOfModelPrefab(componentOrGameObject))
                return PrefabAssetType.Model;

            return PrefabAssetType.Regular;
        }

        public static GameObject LoadPrefabContents(string assetPath)
        {
            if (string.IsNullOrEmpty(assetPath))
                throw new ArgumentNullException("assetPath", "Prefab Asset path is null or empty");

            if (!File.Exists(assetPath))
                throw new ArgumentException(string.Format("Path: {0}, does not exist", assetPath));

            if (Path.GetExtension(assetPath) != ".prefab")
                throw new ArgumentException(string.Format("Path: {0}, is not a prefab file", assetPath));

            var previewScene = EditorSceneManager.NewPreviewScene();
            var rootGameObject = LoadPrefabContentsIntoPreviewScene_Internal(assetPath, previewScene);
            previewScene.SetPathAndGuid(assetPath, AssetDatabase.AssetPathToGUID(assetPath));

            if (rootGameObject == null)
            {
                EditorSceneManager.ClosePreviewScene(previewScene);
                throw new ArgumentException(string.Format("Could not load Prefab contents at path {0}.", assetPath));
            }

            return rootGameObject;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Validate assetPath with !string.IsNullOrEmpty(assetPath) before calling.
  2. Ensure you pass a project-relative path (e.g., 'Assets/Prefabs/MyPrefab.prefab'), not a GUID or absolute path.
  3. If starting from a GUID, use AssetDatabase.GUIDToAssetPath(guid) and check the result is non-empty before passing.

Example fix

// before
var contents = PrefabUtility.LoadPrefabContents(AssetDatabase.GUIDToAssetPath(guid));
// after
var path = AssetDatabase.GUIDToAssetPath(guid);
if (!string.IsNullOrEmpty(path))
    var contents = PrefabUtility.LoadPrefabContents(path);
Defensive patterns

Strategy: validation

Validate before calling

if (!string.IsNullOrEmpty(assetPath))
    var contents = PrefabUtility.LoadPrefabContents(assetPath);

Type guard

static bool IsValidPrefabPath(string path) => !string.IsNullOrEmpty(path) && Path.GetExtension(path) == ".prefab";

Prevention

When it happens

Trigger: Calling PrefabUtility.LoadPrefabContents(null) or PrefabUtility.LoadPrefabContents(string.Empty). The method checks string.IsNullOrEmpty(assetPath) before attempting to load.

Common situations: Passing an AssetDatabase GUID instead of a path, using a path constructed from a missing/null variable, or calling with a path that failed to resolve from AssetDatabase.GUIDToAssetPath (which returns empty string for invalid GUIDs).

Related errors


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