Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Transform.GetSiblingIndex() not working in build?

Discussion in 'Scripting' started by TinyMobGraham, Apr 1, 2016.

  1. TinyMobGraham

    TinyMobGraham

    Joined:
    Oct 17, 2013
    Posts:
    38
    Is Transform.GetSiblingIndex() meant to work in builds? In editor it seems to work fine, but when I do an OS/X build it always returns 0 for every transform. The documentation doesn't give any information.

    I'm trying to search the scene for all objects with a specific component, and then sort the list so it is in the same order for all master/clients connected. Basically the list needs to be deterministic. So I thought I could assign each object an ID based on its index in the hierarchy and sort based on that, but it appears GetSiblingIndex() does not work in builds.

    Am I missing something?
     
  2. TinyMobGraham

    TinyMobGraham

    Joined:
    Oct 17, 2013
    Posts:
    38
    I should state that I am using Unity 5.3.3p3 and am loading scenes asynchronously. Perhaps that is why it doesn't work in builds?
     
  3. TinyMobGraham

    TinyMobGraham

    Joined:
    Oct 17, 2013
    Posts:
    38
  4. 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.             if (child == parent.GetChild(i))
    6.                 return i;
    7.         }
    8.         Debug.LogWarning("Child doesn't belong to this parent.");
    9.         return 0;
    10. }
     
    yuraf_unity and xotam89 like this.
  5. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Quick question.

    When do you call it? Awake? Start? OnEnable?
     
  6. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    @Max-Bot And I should mention: nice necro
     
  7. Max-Bot

    Max-Bot

    Joined:
    Sep 25, 2013
    Posts:
    83
    Doesn't matter where to call. From custom method.

    For further generations.
     
  8. drcrck

    drcrck

    Joined:
    May 23, 2017
    Posts:
    328
    2015-2019, still not fixed.
     
  9. drcrck

    drcrck

    Joined:
    May 23, 2017
    Posts:
    328
    this bug is about root objects, they have no parent. this code won't work
     
    Xtro likes this.
  10. amamaenko

    amamaenko

    Joined:
    Jun 2, 2013
    Posts:
    8
    The year is 2020, Unity 2019.3.10f1, the bug is still there... o_O
     
  11. Havokki

    Havokki

    Joined:
    Jun 28, 2015
    Posts:
    118
    Still there in 2021.1.16. I get correct values in editor, but on build all root gameobjects are at 0. Doesn't matter if I check it in awake/start/update.