Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JigglyWiggly · Apr 09, 2016 at 05:26 PM · gameobjecteditor-scriptingrenamerenaming

Force Rename of GameObject in Hierarchy

Hi all,

I have an editor script that clones an existing GameObject then selects it in the hierarchy. I want to rename the GameObject immediately after selection, mimicing the F2 rename feature.

Any way to do this in code?

Thanks.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JigglyWiggly · Apr 09, 2016 at 03:24 PM 0
Share

If what I'm asking isn't possible, is it at least possible to set Focus to the Hierarchy?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Oribow · Apr 10, 2016 at 10:49 PM

The script posted in the old answer no longer works in newer unity versions. Use this instead:

     using UnityEngine;
     using UnityEditor;
     using System.Reflection;
      
     public class ObjClone : EditorWindow
     {
      
         // Add menu named "My Window" to the Window menu
         [MenuItem("Window/ObjClone")]
         static void Init()
         {
             // Get existing open window or if none, make a new one:
             ObjClone window = (ObjClone)EditorWindow.GetWindow(typeof(ObjClone));
             window.Show();
         }
      
         GameObject g;
         GameObject clone;
         volatile bool triggerRename;
         private static double renameTime;
      
         void OnGUI()
         {
             g = (GameObject)EditorGUILayout.ObjectField(g, typeof(GameObject), true);
             if (GUILayout.Button("Clone"))
             {
                 clone = Instantiate<GameObject>(g);
      
                 EditorApplication.update += EngageRenameMode;
                 renameTime = EditorApplication.timeSinceStartup + 0.4d;
                 EditorApplication.ExecuteMenuItem("Window/General/Hierarchy");
                 Selection.activeGameObject = clone;
             }
         }
      
         private void EngageRenameMode()
         {
             if (EditorApplication.timeSinceStartup >= renameTime)
             {
                 EditorApplication.update -= EngageRenameMode;
                 var e = new Event { keyCode = KeyCode.F2, type = EventType.KeyDown }; // or Event.KeyboardEvent("f2");
                 EditorWindow.focusedWindow.SendEvent(e);
             }
         }
     }
 

OBSOLETE:

Ok. Took me some time, but here you go:

 using UnityEngine;
 using UnityEditor;
 using System.Reflection;
 
 public class ObjClone : EditorWindow
 {
 
     // Add menu named "My Window" to the Window menu
     [MenuItem("Window/ObjClone")]
     static void Init()
     {
         // Get existing open window or if none, make a new one:
         ObjClone window = (ObjClone)EditorWindow.GetWindow(typeof(ObjClone));
         window.Show();
     }
 
     GameObject g;
     GameObject clone;
     volatile bool triggerRename;
     private static double renameTime;
 
     void OnGUI()
     {
         g = (GameObject)EditorGUILayout.ObjectField(g, typeof(GameObject), true);
         if (GUILayout.Button("Clone"))
         {
             clone = Instantiate<GameObject>(g);
 
             EditorApplication.update += EngageRenameMode;
             renameTime = EditorApplication.timeSinceStartup + 0.4d;
             EditorApplication.ExecuteMenuItem("Window/Hierarchy");
             Selection.activeGameObject = clone;
         }
     }
 
     private void EngageRenameMode()
     {
         if (EditorApplication.timeSinceStartup >= renameTime)
         {
             EditorApplication.update -= EngageRenameMode;
             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);
         }
     }
 }
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JigglyWiggly · Apr 11, 2016 at 11:22 PM 0
Share

Holy crap, dude! Works like a charm!

Thanks!!

avatar image Storm4_ · Aug 18, 2020 at 10:13 AM 0
Share

Not sure since when, but "RenameGO" method no longer exists in SceneHierarchyWindow class. I'm amazed how hard it is to initiate rename in script. Ended up creating an EditorWindow with a text field for that.

avatar image Oribow Storm4_ · Aug 18, 2020 at 10:23 AM 0
Share

Updated answer. The idea behind triggering a rename is to fire a keyboard F2 event. This is usually the shortcut to rename stuff. Works well in Unity 2019.

avatar image Storm4_ Oribow · Aug 18, 2020 at 10:31 AM 0
Share

I tried the F2 solution before, could be something with my setup, but it was inconsistent and didn't work at all unless my cursor was inside the Hierarchy window.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there anyway to batch renaming via Editor [not on Runtime] multiple game objects in the hierarchy ? 4 Answers

Add gameObject/transform to script component slot with editor 1 Answer

Best way to rename scripts 1 Answer

Renaming Child Objects during Instantiate? 1 Answer

How to create instance from model without creating a clone? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges