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

I feel like this is a common need for many.
Is there any proper way to rename multiple objects according a linear sequence or anything close to that.

Let’ say for example I drop 100 objects in my scene in a certain order in the hierarchy, how could I rename them as obj1-obj2-obj3 and so on…

Thank you

Yes, it’s possible using a custom editor window. Like this:


using UnityEngine;
using UnityEditor;

public class RenameChildren : EditorWindow {
    private static readonly Vector2Int size = new Vector2Int(250, 100);
    private string childrenPrefix;
    private int startIndex;
    [MenuItem("GameObject/Rename children")] public static void ShowWindow() {
        EditorWindow window = GetWindow<RenameChildren>();
        window.minSize = size;
        window.maxSize = size;
    }
    private void OnGUI() {
        childrenPrefix = EditorGUILayout.TextField("Children prefix", childrenPrefix);
        startIndex = EditorGUILayout.IntField("Start index", startIndex);
        if (GUILayout.Button("Rename children")) {
            GameObject[] selectedObjects = Selection.gameObjects;
            for (int objectI = 0; objectI < selectedObjects.Length; objectI++) {
                Transform selectedObjectT = selectedObjects[objectI].transform;
                for (int childI = 0, i = startIndex; childI < selectedObjectT.childCount; childI++) selectedObjectT.GetChild(childI).name = $"{childrenPrefix}{i++}";
            }
        }
    }
}

To use it:

  1. Save the script in your project.
  2. Go to GameObject/Rename children.
  3. Select the parent of the objects to
    rename.
  4. Fill the fields and press the
    button.
    165209-img-2.png

Thanks @RaptorRush this works perfectly and exactly what i was looking for :slight_smile:

I couldn’t do it can you help me it gives an error[181676-ekran-alıntısı.png|181676]

I thought I would add a way to rename selected objects in case you are not naming the child objects.

using UnityEditor;
using UnityEngine;
 
public class RenameSelected : EditorWindow
{
    // public fields
    public GameObject[] objects;
    
    // private fields
    private static readonly Vector2Int size = new Vector2Int(250, 100);
    private string _gameObjectPrefix;
    private int _startIndex;
    private SerializedObject _serializedObject;
 
    [MenuItem("GameObject/Rename Selected")]
    public static void ShowWindow()
    {
        EditorWindow window = GetWindow<RenameSelected>();
        window.minSize = size;
        window.maxSize = size;
    }
 
    private void OnEnable()
    {
        ScriptableObject target = this;
        _serializedObject = new SerializedObject(target);
    }
 
    private void OnGUI()
    {
        _gameObjectPrefix = EditorGUILayout.TextField("Selected Prefix", _gameObjectPrefix);
        _startIndex = EditorGUILayout.IntField("Start Index", _startIndex);
        
        _serializedObject.Update();
 
        SerializedProperty serializedProperty = _serializedObject.FindProperty("objects");
 
        EditorGUILayout.PropertyField(serializedProperty, true);
 
        if (GUILayout.Button("Rename Objects"))
        {
            
            for (int objectI = 0, i = _startIndex; objectI < serializedProperty.arraySize; objectI++)
            {
                serializedProperty.GetArrayElementAtIndex(objectI).objectReferenceValue.name = $"{_gameObjectPrefix}{i++}";
            }
        }
        
        _serializedObject.ApplyModifiedProperties();
    }
}

This will create an array, in the editor window, simply select the order of the GameObjects, then set your prefix and start index, and hit the Rename Objects button.

-Larry

Heres @RaptorRush’s code but with an added suffix option (btw thanks raptor absolutely saved me):

using UnityEngine;
 using UnityEditor;
 
 public class RenameChildren : EditorWindow 
 {

     private static readonly Vector2Int size = new Vector2Int(250, 100);

     private string childrenPrefix;
     private int startIndex;
     private string childrenSuffix;

     [MenuItem("GameObject/Rename children")] public static void ShowWindow() 
     {
         EditorWindow window = GetWindow<RenameChildren>();
         window.minSize = size;
         window.maxSize = size;
     }

     private void OnGUI() 
     {
        childrenPrefix = EditorGUILayout.TextField("Children prefix", childrenPrefix);
        childrenSuffix = EditorGUILayout.TextField("Children suffix", childrenSuffix);

        startIndex = EditorGUILayout.IntField("Start index", startIndex);

        if (GUILayout.Button("Rename children")) 
        {
            GameObject[] selectedObjects = Selection.gameObjects;
            for (int objectI = 0; objectI < selectedObjects.Length; objectI++) 
            {
                Transform selectedObjectT = selectedObjects[objectI].transform;
                for (int childI = 0, i = startIndex; childI < selectedObjectT.childCount; childI++) selectedObjectT.GetChild(childI).name = $"{childrenPrefix}{i++}{childrenSuffix}";
            }
        }
    }
}