Unity-Technologies/UnityCsReference · error · ArgumentException

Cannot save as new prefab using the same path

Error message

Cannot save as new prefab using the same path

What it means

Thrown by PrefabStage.SaveAsNew when newPath equals the current m_PrefabAssetPath. Saving as a new prefab requires a distinct destination path; reusing the current path is an error because SaveAsNew is meant to create a new asset, not overwrite the existing one (use Save() for that).

Source

Thrown at Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStage.cs:1503

                m_IgnoreNextAssetImportedEventForCurrentPrefab = false;
            }

            if (SceneHierarchy.s_DebugPrefabStage)
                Debug.Log("SAVE PREFAB ended");

            showingSavingLabel = false;

            return savedSuccesfully;
        }

        internal bool SaveAsNew(string newPath, bool asCopy)
        {
            if (!isValid)
                return false;

            if (newPath == m_PrefabAssetPath)
            {
                throw new ArgumentException("Cannot save as new prefab using the same path");
            }

            if (asCopy)
            {
                // Keep current open prefab and save copy at newPath
                string oldName = m_PrefabContentsRoot.name;
                m_PrefabContentsRoot.name = Path.GetFileNameWithoutExtension(newPath);
                PrefabUtility.SaveAsPrefabAsset(m_PrefabContentsRoot, newPath);
                m_PrefabContentsRoot.name = oldName;
            }
            else
            {
                // Change the current open prefab and save
                m_PrefabContentsRoot.name = Path.GetFileNameWithoutExtension(newPath);
                m_PrefabAssetPath = newPath;
                CachePrefabFolderInfo();
                ClearDirtiness();
                PrefabUtility.SaveAsPrefabAsset(m_PrefabContentsRoot, newPath);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure newPath is a different path than the current prefab asset path; use a distinct filename or folder.
  2. If you intend to overwrite the existing prefab, call Save() instead of SaveAsNew().
  3. Normalize both paths (Path.GetFullPath) before comparison if path formatting differs.

Example fix

// before
stage.SaveAsNew(stage.assetPath, asCopy: false);

// after
string newPath = stage.assetPath.Replace(".prefab", "_Variant.prefab");
if (newPath != stage.assetPath)
    stage.SaveAsNew(newPath, asCopy: false);
Defensive patterns

Strategy: validation

Validate before calling

if (string.Equals(newPath, stage.assetPath, StringComparison.OrdinalIgnoreCase))
    throw new InvalidOperationException("Use Save() to overwrite the current prefab.");

Prevention

When it happens

Trigger: Calling SaveAsNew with the same path as the currently open prefab. Path comparison failing to normalize, e.g., passing a path that resolves to the same asset after canonicalization but differs textually (though the guard does exact string comparison).

Common situations: Automated tooling that derives newPath from the current asset path and accidentally lands on the same value. Copy/paste of a path without changing the filename. UI flow where 'Save As' defaults to the current path.

Related errors


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