Unity-Technologies/UnityCsReference · error · ArgumentNullException

templatePath

Error message

templatePath

What it means

ProjectWindowUtil.CreateScriptAssetFromTemplateFile throws an ArgumentNullException when templatePath is null. This method creates a new script asset from a template file (e.g., creating a new MonoBehaviour or Shader via menu), and requires a valid path to the template.

Source

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

            var action = ScriptableObject.CreateInstance<DoCreateAssetWithContent>();
            action.filecontent = content;
            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;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass a valid template path, typically under the Unity editor templates folder or a package path.
  2. Validate templatePath != null before calling.
  3. If referencing a package template, ensure the package is installed and the path is correct (e.g., 'Packages/com.unity.terrain-tools/Editor/Templates/...').
  4. Use EditorGUIUtility.isProSkin or EditorApplication.applicationPath to construct template paths robustly.

Example fix

// before
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(null, "NewBehaviourScript.cs");
// after
var templatePath = "Assets/Editor/Templates/C# Script-NewBehaviourScript.cs.txt";
if (templatePath != null && File.Exists(templatePath))
    ProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, "NewBehaviourScript.cs");
Defensive patterns

Strategy: validation

Validate before calling

if (templatePath != null)
    ProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, defaultName);

Type guard

static bool IsValidTemplatePath(string path) => path != null && File.Exists(path);

Prevention

When it happens

Trigger: Calling ProjectWindowUtil.CreateScriptAssetFromTemplateFile(null, defaultName) where the template path argument is null.

Common situations: Custom menu items that create scripts from templates but pass a null path due to a misconfigured template location, or when the template path comes from a ScriptableObject field that was left empty. Also happens when template files are expected in a package but the package isn't installed.

Related errors


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