Unity-Technologies/UnityCsReference · error · FileNotFoundException
The template file "{templatePath}" 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.
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)
Solutions
- Verify the file exists with File.Exists(templatePath) before calling.
- Use absolute or AssetDatabase-relative paths rather than relative filesystem paths.
- If using package templates, check the package is installed: PackageUtility.CheckForInvalidPath or Directory.Exists on the package folder.
- 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
- Verify File.Exists on template paths before calling
- Use absolute or project-relative paths consistently
- Check that template-shipping packages are installed
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
- templatePath
- No such file or directory.
- The SerializedProperty '{0}' is not supported by BeginProper
- The MaterialProperty '{0}' should be of type 'Texture' (its
- List of materials contains null
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/db3bd596340b3a83.
Report an issue: GitHub.