How can i create shortcut for creating new c# script and adding it to selected gameoject in editor?

I am trying to write a editor script that can create a new c# script and add it to the gameobject selected in the editor.I only have rough idea that i need to use reflection.

You can try Ctr+Shift+A for windows when selecting a gameobject. It is used to add components, just type the script you want to create then and I guess it’s the same use case then. The drawback is that you can’t specify the folder. It creates the script in Assets folder directly I think.

Unity doesn’t offer default way that you can create scripts with shortcut.
But there are two ways of doing it.
First way you can write your own script. Then put it in ‘Editor’ folder.
Second way is using Shortcuts window to assign your own shortcut to create new C# script.
1. First way (Custom editor script)
I made this script that creates new Script file in selected path using Ctrl + Alt + N command.

  1. Create ‘Editor’ folder in Assets

  2. Create new script inside and name it ‘CreateScript.cs’

  3. Open script then add this code in.

    using UnityEditor;
    using UnityEngine;
    public class CreateScript: EditorWindow
    	[MenuItem("Component/Scripts/New Script %&n")]
    	public static void ShowWindow()
    	private bool autoFocus = true;
    	private string scriptName = "NewScript";
    	private void OnGUI()
    		GUILayout.Label("Custom Script Creator", EditorStyles.boldLabel);
    		scriptName = EditorGUILayout.TextField("Script Name", scriptName);
    		if (autoFocus)
    			autoFocus = false; // Disable autofocus after the first frame
    		if (GUILayout.Button("Create Script") || Event.current.keyCode == KeyCode.Return)
    	private void CreateNewScript()
    		// Get the currently selected folder in the Unity Editor
    		string selectedFolder = "Assets";
    		Object selectedObject = Selection.activeObject;
    		if (selectedObject != null)
    			string selectedPath = AssetDatabase.GetAssetPath(selectedObject.GetInstanceID());
    			if (!string.IsNullOrEmpty(selectedPath))
    				selectedFolder = selectedPath;
    				if (!System.IO.Directory.Exists(selectedPath))
    					selectedFolder = System.IO.Path.GetDirectoryName(selectedPath);
    		string scriptPath = System.IO.Path.Combine(selectedFolder, scriptName + ".cs");
    		// Create the script file with a default template
    		string template = "using UnityEngine;

public class {0} : MonoBehaviour
// Start is called before the first frame update
void Start()


// Update is called once per frame
void Update()


string scriptContent = string.Format(template, scriptName.Replace(” ", “”), scriptName);

    		System.IO.File.WriteAllText(scriptPath, scriptContent);
    		// Select the newly created script in the Unity Editor
    		Object createdScript = AssetDatabase.LoadAssetAtPath(scriptPath, typeof(Object));
    		Selection.activeObject = createdScript;
  1. Save it.

Before using be sure that you selected all of these checkmarks

Now you can easily create script file in any path you want using Ctrl + Alt + N. Hope this helps!

Only cons of this method is you should always keep this script in your project in order to work but the props is you can create pre-selected buttons for window such as derive from Scriptable object or Editor window. Also you can add little field to add your script from beginning without opening script again.

If you sick of setting this up you can just download this package to use. It contains same code as this.

2. Second way (Edit => shortcuts)
This way is quicker and default way. Just go Edit => Shortcuts. Click on tiny narrow search bar and write down “script” then double click it to prepare for assigning new shortcut. Enter your custom shortcut. Don’t worry about replacing other shortcut because unity will warn you this shortcut is already using by another action. (In my case I used Ctrl + Alt + L since Ctrl + Alt + N is for custom C# script).