Search Unity

Bug : GetSiblingIndex always returns zero on scene root game objects in build runtime.

Discussion in 'Editor & General Support' started by Xtro, Jan 14, 2015.

  1. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    I reported this bug via Unity bug reporter tool long time ago but it was never fixed.

    Bug record:
    http://fogbugz.unity3d.com/default.asp?653948_lhpsonb8akqhg7n7

    Issue tracker:
    https://issuetracker.unity3d.com/issues/getsiblingindex-always-returns-zero-in-standalone-builds

    Bug description:
    GetSiblingIndex always returns zero on scene root game objects in build runtime.

    It's working good for parented objects.
    In editor play mode runtime, it's working perfect for both root and parented objects. The only problem is in build runtime for only root objects.

    Steps: (test project was included in bug record when I reported it via bug reporter tool)
    1) Open the test scene in editor
    2) Start the play mode and see the sibling index is 1.
    3) Build the project and run it.
    4) Notice the sibling index is zero which is a huge bug!!!
     
    Last edited: Jan 25, 2016
  2. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    Can I get attention from a Unity Team member please? :)
     
  3. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    Bump :(
     
  4. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    It's been days but I couldn't get any response from the Unity Team :( :(
     
  5. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    Hello... I'm talking about a serious API bug here.
     
  6. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
  7. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    I verified it with 4.6.1p2 but I did't see any patch note about it for the other patches. Also, my bug report record is still "open"

    Do you want me to verify it on 4.6.1p4?

    And, why can't I find this bug in issue tracker?
     
  8. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    Yes please if you could just to make sure.

    I actually had passed this on but let me give the guys another gentle prod.
     
  9. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    Verified. It still exists in 4.6.1p4
     
  10. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    Will I be able to see it in issue tracker soon? Because trying to track it on the forum is not so cool :)
     
  11. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    I know Unity team is very busy but...
    Can I get an update about this serious API bug?
    I reported it months ago. :(
     
  12. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
  13. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
  14. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    Also, SetSiblingIndex is not working for gameObjects in the root (Unity 5.2.1)
     
  15. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    Can we get an update from UT please? Is there a specific reason for ignoring this very obvious API bug?
     
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    Its not being ignored. I will poke the core team on Monday and see what the status is.
    Can you file a bug report for this please.
     
  17. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
  18. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    @karl.jones

    Were you able to poke the core team about the status?
     
  19. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    Sorry Yes I was. Its not actually a bug as such, its using editor only features and so disabled at run time. We basically have a scene root object that does not exist outside of the editor. This could potentially be addressed with the future changes being made to the scene system at the moment but its not something that can be quickly fixed. Its now back with the core team to look at. As a workaround whilst you wait you could create a single GameObject as a scene root if you needed to replicate the behavior.
     
  20. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    That's very bad to hear :( Yes I'm aware of the workaround which is very ugly :(

    Btw, why would a runtime API would use editor feature by default?
     
  21. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    I don't know, I was a little surprised myself. :(
    I think its something the UI team introduced so I guess they may know.
     
  22. vsugrob

    vsugrob

    Joined:
    Mar 9, 2014
    Posts:
    5
    codestage likes this.
  23. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    It's been YEARS!!!
     
  24. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    It's also worth mentioning that this behaviour also breaks Unity's own Hierarchy order (or at least I suppose it's connected to it). Which means that the root order in the Hierarchy is never saved in a scene, and each time you reopen it all root parents are randomized, which is super-annoying :(
     
  25. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,931
    Yeah, this is pretty annoying and misleading "feature" =(
    It should be mentioned in docs as editor-only feature for the scene root objects.
     
  26. Max-Bot

    Max-Bot

    Joined:
    Sep 25, 2013
    Posts:
    83
    You can use own implementation:
    Code (CSharp):
    1. int GetSiblingIndex(Transform child, Transform parent)
    2.     {
    3.         for (int i = 0; i < parent.childCount; ++i)
    4.         {
    5.             Debug.Log(child.GetInstanceID() + " - " + parent.GetChild(i).GetInstanceID());
    6.             if (child.GetInstanceID() == parent.GetChild(i).GetInstanceID())
    7.                 return i;
    8.         }
    9.         Debug.LogWarning("Child doesn't belong to this parent.");
    10.         return 0;
    11.     }
     
  27. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    We are talking about a bug here. Why did you think I would need a workaround for it and why did you think I can't implement it myself?
     
  28. Max-Bot

    Max-Bot

    Joined:
    Sep 25, 2013
    Posts:
    83
    It's a public post for people who looking for solutions as well.
    I'm sure you are a cool dude and can.
     
  29. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    71
    Hmmm… this seems not to get a fix anytime soon… my current workaround is this:
    Code (CSharp):
    1. [SerializeField][HideInInspector]
    2. private int sceneIndex;
    3.  
    4. #if UNITY_EDITOR
    5. void OnValidate()
    6. {
    7.     sceneIndex = transform.GetSiblingIndex();
    8. }
    9. #endif
    10.  
    (Put this on components that need to know the sceneIndex at runtime in builds)
     
    any_user likes this.
  30. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Wow.

    Just ran into this myself and I'm stunned that not only did Unity never fix it, they never even added a warning to either the documentation, or when making a build or when trying to get the sibling of root object in the editor or a build.

    This is a really insidious hidden gotcha just waiting to bite you on the ass, more so since it works as expected in the editor! At the very least you should get a warning in the editor when using this on a root level transform.

    This has given me massive amounts of stress as i'm up against a deadline to upload builds to the stores. At the last minute I had to make a change that required using getsiblingindex() of child transforms of the root. In the editor all was well, but my builds completely broke in weird ways and its taken a good hour or more to track it down and confirm it. Didn't help that for some reason I could make a development build for Android, so was pure luck I tried a windows build and found an error message regarding duplicate keys in a sortedList as the siblingIndex always returned zero.
     
    Joe-Censored likes this.
  31. benoitd_unity

    benoitd_unity

    Unity Technologies

    Joined:
    Jan 2, 2018
    Posts:
    331
    I'll find who could take a look at this, I agree it's pretty insidious.
     
    Demigiant, Noisecrime and karl_jones like this.
  32. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Thanks.

    Thinking about it, my preferred method to address this would be an editor check if the transform is a root object and if so produce a warning or error on use. Anything else like just updating the documentation isn't going to be much help when a developer might not realize what is causing bugs in the first place! Though it'd also like to see root level transforms just return say -1 for siblingIndex since that would be pretty clear indicator that its not supported.
     
  33. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    Oh, so even in 2021 this still not fixed?
     
  34. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    In issue tracker page, Unity team says:

    Postponed means that the issue was either a feature request or something that requires major refactoring on our side. Since that makes the issue not actionable in the close future we choose to close it as Postponed and add it on our internal roadmaps and technical debt pages instead.

    This is very sad. :(
     
    Thaina, Noisecrime and codestage like this.
  35. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    Just tripped on this for a couple hours today (w/ 2022.1). Unity, if no one can fix it, please at least document it in both the manual and API comments.

    But really, please fix it. This is a landmine, destroyer of time and souls.
     
    Thaina and Noisecrime like this.
  36. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    610
    Unity, please see @Creepgin 's suggestions above.........
     
  37. Hinne123

    Hinne123

    Joined:
    May 29, 2018
    Posts:
    3
    I stumbled across the problem and implemented this workaround:

    Code (CSharp):
    1. public static class HierarchyHelper
    2.     {
    3.         /// <summary>
    4.         /// In a build the sibiling index of a root object is always returned as 0.
    5.         /// This methods checks if the transform is a root object, and if so, determines the correct sibling index
    6.         /// and returns this instead.
    7.         /// </summary>
    8.         /// <param name="transform"></param>
    9.         /// <returns></returns>
    10.         public static int GetBuildSaveSiblingIndex(this Transform transform)
    11.         {
    12.             if (transform.parent != null) return transform.GetSiblingIndex();
    13.             var rootGameObjectTransforms = SceneManager.GetActiveScene()
    14.                 .GetRootGameObjects()
    15.                 .Select(go => go.transform)
    16.                 .ToArray();
    17.            
    18.             return Array.IndexOf(rootGameObjectTransforms, transform);
    19.         }
    20.     }
    I just tested it on Windows Standalone...