Unity Custom Menu Problem

Hello,

I want to rename all the child objects in sequential order (Object.0, Object.1, Object.2 etc) in the unity editor inspector.

So I wrote a script to add a menu item in the unity editor, that takes the items I selected in the inspector, and renames them.

However the selected objects returned by unity seems to be in random order, so when the script is executed, the items end up being renamed not from top to bottom, but in random order.

For example if I select 10 sequential items in the inspector, then execute the script I expect them to be renamed xx.0,xx.1,xx.2,xx.3, and so on, but the end up being xx.3,xx.1,xx.0,xx.5,xx.2 etc

Anybody knows why

Here is the script

using UnityEngine;
using UnityEditor;
using System.Collections;

 public class Menu : MonoBehaviour {

[MenuItem("Tools/Batch Rename")]
private static void BatchRename()
{
    int icount = 0;

    // first chop off any numbers from the object name end
    for (int i=0; i< Selection.gameObjects.Length; i++) {
        GameObject igo = Selection.gameObjects*;*

string[] sn = igo.name.Split(new char[] { ‘-’, ‘.’, ’ ', ‘,’, ‘.’, ‘:’, ’ ', ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘0’ });
if (sn.Length > 1) {
igo.name = sn[0];
}
}
// append sequential number to end of name
icount = 0;
for (int i = 0; i < Selection.gameObjects.Length; i++) {
GameObject igo = Selection.gameObjects*;*
igo.name = igo.name + “.” + icount;
icount++;
}
}
}

It’s a old question, but I found a way of doing this and I think I should post it here in case someone needs it.

	for (int i = 0; i < Selection.activeTransform.parent.childCount; i++)
	{
		Selection.activeTransform.parent.GetChild(i).name = i + "";
	}

The GetChild(i) returns the child objects in sequential order from first child to last.

You just have to select one child object and it will rename all the child objects in it’s parent object.