Unity-Technologies/UnityCsReference · error · FileNotFoundException

The template file " " could not be found.

Error message

The template file "{templatePath}" could not be found.

What it means

ProjectWindowUtil.CreateScriptAssetFromTemplateFile throws a FileNotFoundException when the template file at templatePath does not exist on disk. The method verifies File.Exists(templatePath) before proceeding with script generation.

Solutions

  1. Verify the file exists with File.Exists(templatePath) before calling.
  2. Use absolute or AssetDatabase-relative paths rather than relative filesystem paths.
  3. If using package templates, check the package is installed: PackageUtility.CheckForInvalidPath or Directory.Exists on the package folder.
  4. For built-in templates, use the correct Unity version's template path.

Example fix

// before
ProjectWindowUtil.CreateScriptAssetFromTemplateFile("Assets/Templates/Missing.cs.txt", "NewScript.cs");
// after
var templatePath = "Assets/Editor/Templates/ScriptTemplate.cs.txt";
if (!File.Exists(templatePath))
    Debug.LogError($"Template not found: {templatePath}");
else
    ProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, "NewScript.cs");
Defensive patterns

Strategy: validation

Validate before calling

if (templatePath != null && File.Exists(templatePath))
    ProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, defaultName);
else
    Debug.LogError($"Template file not found: {templatePath}");

Type guard

static bool TemplateExists(string path) => !string.IsNullOrEmpty(path) && File.Exists(path);

Prevention

When it happens

Trigger: Calling CreateScriptAssetFromTemplateFile with a path to a template file that doesn't exist on the filesystem.

Common situations: Custom template files that were deleted or moved, package templates where the package was updated/removed, or hardcoded relative paths that resolve incorrectly depending on working directory. Common when upgrading Unity versions that changed template locations.

Related errors


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

Appendix: source

Thrown at Editor/Mono/ProjectBrowser/ProjectWindowUtil.cs:751

            action.onComplete = onRenameComplete != null ? (id) => onRenameComplete(id) : null; // Wrap to obsolete int version
            StartNameEditingIfProjectWindowExists(EntityId.None, action, filename, icon, null);
        }

        public static void CreateAssetWithTextContent(string filename, string content, Texture2D icon = null, Action<EntityId> onRenameComplete = null)
        {
            var action = ScriptableObject.CreateInstance<DoCreateAssetWithContent>();
            action.filecontent = content;
            action.onComplete = onRenameComplete;
            StartNameEditingIfProjectWindowExists(EntityId.None, action, filename, icon, null);
        }

        [RequiredByNativeCode]
        public static void CreateScriptAssetFromTemplateFile(string templatePath, string defaultNewFileName)
        {
            if (templatePath == null)
                throw new ArgumentNullException(nameof(templatePath));
            if (!File.Exists(templatePath))
                throw new FileNotFoundException($"The template file \"{templatePath}\" could not be found.", templatePath);

            if (string.IsNullOrEmpty(defaultNewFileName))
                defaultNewFileName = Path.GetFileName(templatePath);

            Texture2D icon = null;
            switch (Path.GetExtension(defaultNewFileName))
            {
                case ".cs":
                    icon = EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D;
                    break;
                case ".shader":
                    icon = EditorGUIUtility.IconContent<Shader>().image as Texture2D;
                    break;
                case ".asmdef":
                    icon = EditorGUIUtility.IconContent<AssemblyDefinitionAsset>().image as Texture2D;
                    break;
                case ".asmref":
                    icon = EditorGUIUtility.IconContent<AssemblyDefinitionReferenceAsset>().image as Texture2D;

View on GitHub (pinned to 225b0fbdb5)