Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Does GetComponentIndex always return 0 for Transform or RectTransform

Discussion in 'Scripting' started by aaefiikmnnnr, May 15, 2024.

  1. aaefiikmnnnr

    aaefiikmnnnr

    Joined:
    Aug 21, 2014
    Posts:
    13
    I was testing new Unity 6 GetComponentIndex API, and it seems Transform and RectTransform would always get index 0, but it's not backed up by documentation. I would like to ask is it the case that Transform and RectTransform are always index 0? If so could we have it documented. Thank you.
     
    dklassic likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    It seems likely, since that is always the first component on the GameObject. I would think this returns the ordinal of the component in the inspector window.
     
    Bunny83 and Lekret like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,210
    Right. Even in the past where the order of the components wasn't really guaranteed, the Transform component was always the first one. Could have been just an artifact because it's always the first one created / added. Though since Unity now guarantees the order of components (I think since the introduction of AudioFilters which are applied in order) I'm also 99.9% sure that the index will most likely represent the actual position in the inspector.

    As you might know you can move components up / down from the context menu. However the Transform stays fix at the top.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,578
    The docs writers have assured me that they actually do read and act on the little "Report a problem on this page" reports, so feel free say what information would be great to include on that.