Unity generates new scripts from template files, and looks in two places: Assets/ScriptTemplates in your project, which takes priority, and the built-in templates in the Editor installation. To change what new scripts contain, copy a built-in template into Assets/ScriptTemplates keeping its file name, and edit it. Keep the #SCRIPTNAME#, #NOTRIM# and #ROOTNAMESPACEBEGIN#/#ROOTNAMESPACEEND# keywords. Applies to Unity 6 and later.
Applies to: Unity 6 (6000.x) and later. Template file names are different in Unity 2022 LTS and earlier.
Symptoms
- When I create a new script, the Unity Editor fills it with default content. I want every new script to start from my own template instead.
- My script template stopped working after I upgraded to Unity 6.
Cause
New scripts are generated from template files. Unity looks for templates in two places:
-
Your project:
Assets/ScriptTemplates. Templates here apply to that project only and take priority over the built-in ones. - The Editor installation: the built-in templates that ship with each Unity version.
Resolution
Recommended: add templates to your project
A project template survives Editor upgrades, is shared with your team through version control, and does not need administrator rights.
- In the Project window, create a folder named exactly
ScriptTemplatesdirectly underAssets. -
Copy the built-in template you want to change from the Editor installation into
Assets/ScriptTemplates. The paths are listed under Alternative: change the templates for every project. Keep the file name unchanged. For a MonoBehaviour script, it is:1-Scripting__MonoBehaviour Script-NewMonoBehaviourScript.cs.txt
- Edit the copy in any text editor.
- Create a script with Assets > Create > Scripting > MonoBehaviour Script, or right-click in the Project window and select Create > Scripting > MonoBehaviour Script.
- If the change does not appear, restart the Unity Editor and create the script again.
A project template with the same file name as a built-in one replaces that menu entry. A template with a new file name adds a new menu entry.
How the file name maps to the menu
1-Scripting__MonoBehaviour Script-NewMonoBehaviourScript.cs.txt
│ │ │ │ │
│ │ │ │ └ extension of the created file (.cs)
│ │ │ └ default name of the new file
│ │ └ menu item name
│ └ submenu under Assets > Create ("__" separates submenu levels)
└ sort order in the menu
For example, 90-Scripting__My Team Script-NewTeamScript.cs.txt adds Create > Scripting > My Team Script, which creates NewTeamScript.cs.
Template keywords
| Keyword | Replaced with |
|---|---|
#SCRIPTNAME# |
The file name you type, used as the class name. Keep it — without it, the class name won't match the file name and Unity can't attach the script to a GameObject. |
#NOTRIM# |
Nothing. It stops Unity from removing the whitespace on that line, so empty method bodies keep their blank line. |
#ROOTNAMESPACEBEGIN# / #ROOTNAMESPACEEND#
|
namespace <Root Namespace> { and }. The value comes from Edit > Project Settings > Editor > C# Project Generation > Root namespace, or from the assembly definition (.asmdef) that owns the folder. If no root namespace is set, Unity removes both lines. |
The built-in MonoBehaviour template in Unity 6 is:
using UnityEngine;
#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
#NOTRIM#
}
// Update is called once per frame
void Update()
{
#NOTRIM#
}
}
#ROOTNAMESPACEEND#
Tip: Keep
#ROOTNAMESPACEBEGIN#indented by four spaces, as in the built-in file. Unity uses that indentation to indent the class correctly when it wraps it in a namespace.
Here is an example of a modified template:
// Modified template by Unity Support.
using UnityEngine;
#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : MonoBehaviour
{
#region Serialized Fields
#endregion
#region Unity Methods
void Start()
{
#NOTRIM#
}
void Update()
{
#NOTRIM#
}
#endregion
#region Private Methods
#endregion
}
#ROOTNAMESPACEEND#
Alternative: change the templates for every project
You can also edit the templates inside the Editor installation. This affects every project opened with that Editor version. On Windows it needs administrator rights, and it is overwritten whenever you install or update that Editor version. The default Unity Hub locations are:
| OS | Path |
|---|---|
| Windows | C:\Program Files\Unity\Hub\Editor\<version>\Editor\Data\Resources\ScriptTemplates |
| macOS | /Applications/Unity/Hub/Editor/<version>/Unity.app/Contents/Resources/ScriptTemplates |
| Linux | ~/Unity/Hub/Editor/<version>/Editor/Data/Resources/ScriptTemplates |
On macOS, right-click Unity.app and select Show Package Contents to reach Contents/Resources. If you changed the Hub's install location, replace the start of the path accordingly.
Keep a copy of your modified templates outside the Unity installation, and back up the originals before you edit them.
The Unity 6 built-in templates for C# scripts include:
1-Scripting__MonoBehaviour Script-NewMonoBehaviourScript.cs.txt2-Scripting__ScriptableObject Script-NewScriptableObjectScript.cs.txt3-Scripting__Empty C# Script-NewEmptyCSharpScript.cs.txt
Upgrading from Unity 2022 LTS or earlier
Unity 6 renamed the built-in script templates. An override file with an old name is ignored without any error, so a template that worked before an upgrade simply stops having any effect, and it can look as though your edit did not save.
If your customization stopped working, rename your file:
| Old name | Unity 6 name |
|---|---|
81-C# Script-NewBehaviourScript.cs.txt |
1-Scripting__MonoBehaviour Script-NewMonoBehaviourScript.cs.txt |
82-Javascript-NewBehaviourScript.js.txt |
No longer applies. UnityScript was removed in Unity 2018.2. |
83-Shader__Standard Surface Shader-NewSurfaceShader.shader.txt |
1-Shader__Standard Surface Shader-NewSurfaceShader.shader.txt |
84-Shader__Unlit Shader-NewUnlitShader.shader.txt |
2-Shader__Unlit Shader-NewUnlitShader.shader.txt |
Also check the body of your template. The older default used using System.Collections; and a // Use this for initialization comment; the Unity 6 default does not.