Sending a "Rename" CommandEvent to the Hiearchy window, almost there...

This is some what related to this question - but not about sending keys, but about the functionality I wanted from sending the keys.

I’m still looking for a way to engage in rename mode in the hierarchy via code - maybe get an access to the context menu that appears (which has Rename, Duplicate, Copy, Paste, etc)

So I got close this time, with the help of @Jamora, he told me about EditorGUIUtility.CommandEvent - All I needed to do now, is to find a way to give focus to the hierarchy window, I managed to do this via executing the menu item “Window/Hiearchy” - And indeed, I managed to access those options!

		var go = new GameObject("[Generated] Temp");
		EditorApplication.ExecuteMenuItem("Window/Hierarchy"); // focus on the hierarchy window
		EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent("Duplicate")); // send an event, to the current focused window, which is the hierarchy at this point...

(All SelectObject does is Selection.objects = new Object[] { obj };)

Duplicate, Copy, Paste, etc all of these work, except for Rename! Give me a break!

For some reason it just won’t work, nothing happens when I send a Rename, the hierarchy gets focus and,… nothing else.

I tried “Rename”, “rename”, “RENAME” no dice.

Any ideas why rename mode isn’t engaging (while it should)?



Thanks to @ArkaneX - I was able to send a rename event, but for some reason sending the event immediately after I create a new GO, doesn’t seem to do anything (doesn’t engage rename mode):

	if (GUILayout.Button("Create")) {
		var go = new GameObject("[Generated] New GO");


public static void EngageRenameMode(Object go)

public static void SelectObject(Object obj)
	Selection.objects = new Object[] { obj };

public static EditorWindow GetFocusedWindow(string window)
	return EditorWindow.focusedWindow;

public static void FocusOnWindow(string window)
	EditorApplication.ExecuteMenuItem("Window/" + window);

public static class Events
	public static Event Rename = new Event() { keyCode = KeyCode.F2, type = EventType.KeyDown };

Again, this only doesn’t seem to work when I’m creating a new GO and then renaming. Not sure why…

Hmmm. There’s no Edit\Rename command in my version of Unity. Are you sure you have this command?

But you can do this using F12. I answered your original question, but posting solution here as well:

var e = new Event { keyCode = KeyCode.F2, type = EventType.keyDown }; // or Event.KeyboardEvent("f2");

EDIT: I think the problem might be related to the fact, that adding a game object (internally, by Unity Editor) is a complex process, which involves many steps and requires some time to fully finish. I was able to workaround this, but you have to either seek a way to determine when the object is fully created in editor or live with an arbitrary delay. I used a delay of 0.2s, but for example 0.1s was too low in my environment - you have to experiment.

public class EventsTest : EditorWindow
    private static GameObject go;
    private static double renameTime;

    private static void Test ()
        go = new GameObject("[Generated] New GO");
        renameTime = EditorApplication.timeSinceStartup + 0.2d;
        EditorApplication.update += EngageRenameMode;

    private static void EngageRenameMode()
        if (EditorApplication.timeSinceStartup >= renameTime)
            EditorApplication.update -= EngageRenameMode;

Here’s a solution via reflection. This is the same method that gets called when you F2 or Right Click | Rename.

public static void Rename()
    var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
    var hierarchyWindow = EditorWindow.GetWindow(type);
    var rename = type.GetMethod("RenameGO", BindingFlags.Instance | BindingFlags.NonPublic);
    rename.Invoke(hierarchyWindow, null);

This answer is about engaging rename mode in the Project Window. The question is about the Hierarchy Window. I’m putting it here because it shows up high on google when looking up renaming assets in the project window.

The most common reason for wanting to do this is probably because you’re creating a new asset and want the user to be able to edit it. This can now be done using a utility method. UnityEditor.ProjectWindowUtil.CreateAsset(UnityEngine.Object asset, string pathName).

The ProjectWindowUtil class is undocumented, but public. This method appears to handle the whole process of adding the asset to the database, as well as selecting it and going into rename mode just like happens when you use the menu to create an asset.

At current point (2021.3.8f1) the solution of @vexe wont work. because UnityEditor.CoreModule.dll → UnityEditor.SceneHierarchyWindow → RenameGO method not exist. But!! that type contains a member of type SceneHierarchy that can help us create new solution for “Enter renaming mode on created gameObjects”. the solution is on the gist: Unity Editor: Renaming game object hook · GitHub