Search Unity

Transform SiblingIndex on prefabs not saving/loading correctly

Discussion in 'Scripting' started by SagoRich, Aug 1, 2014.

  1. SagoRich

    SagoRich

    Joined:
    Jul 21, 2014
    Posts:
    15
    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.

    Code (csharp):
    1. GameObject goRoot = new GameObject("Root");
    2. for (int i=0; i<15; i++) {
    3.     GameObject goChild = PrefabUtility.InstantiatePrefab(somePrefab) as GameObject;
    4.     goChild.name = string.Format("{0:D2}_{1}", i, goChild.name);
    5.     childPrefab.transform.parent = goRoot.transform;
    6.     childPrefab.transform.SetSiblingIndex(i);
    7. }
    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).
     
  2. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    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.
     
  3. SagoRich

    SagoRich

    Joined:
    Jul 21, 2014
    Posts:
    15
    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.
     
  4. Deadcow_

    Deadcow_

    Joined:
    Mar 13, 2014
    Posts:
    135
    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 :)

    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