Transform SiblingIndex on prefabs not saving/loading correctly

I just encountered this, and since the indexing is relatively new, I’m not sure if I am using it wrong or what, but I thought I’d ask.

GameObject goRoot = new GameObject("Root");
for (int i=0; i<15; i++) {
    GameObject goChild = PrefabUtility.InstantiatePrefab(somePrefab) as GameObject;
    goChild.name = string.Format("{0:smile:2}_{1}", i, goChild.name);
    childPrefab.transform.parent = goRoot.transform;
    childPrefab.transform.SetSiblingIndex(i);
}

Basically, at editor time, spawn an object and attach some prefabs. Save the scene, New Scene, Re-open the saved scene and note the order – probably not the same order. Repeat, and different again. Printing out the SiblingIndex of each shows they have changed on load (and each re-load).

We have no custom hierarchy sorts.

This seems like a bug, but then again, I’m not sure if there is some special handling of prefab serialization that overrides the indexing (deliberately).

It’s probably just that Unity’s serialization doesn’t preserve the sibling order. SetSiblingIndex isn’t well documented, so I’d hazard a guess that it’s just tacked on. I’d use the names rather than the index, myself; it makes things easier to find in the editor, anyway.

Thanks for the reply.
I’m fairly sure the intent is that you can set the sort order and save it (preparation for GUI changes in 4.6), and it seems to save it fine if the items are not prefabs.

This issue is still present in 5.3.2p2. Is someone know how accurately save transform hierarchy sorting order?
Sorting order is messed every time I enter and exit Playmode and not saved with scene either.
Hell of a perfectionist :slight_smile:

I’m about to make a tool, that will save/restore SiblingIndex and store this data in scriptable object, but still hope to find native way to do this.

Anyways, hierarchy sorting is newer a problem with UI (where it needed the most - works fine), but with scene root objects seemed to be random