-1

How can i change index of an object at runtime? an object's index changing in hierarchy. because I'm using reorderableList in game. you can see at Unity UI Extension asset. when index of an object changing in hierarchy, it will change index at array too.

Image Attachment

NnN
  • 463
  • 2
  • 11
freedom667
  • 29
  • 3
  • 9
  • can't you just track the order without the index based on the position? – Sebastian L Feb 20 '18 at 10:20
  • Wlecome to SO. Did you take [the tour](https://stackoverflow.com/tour)? I am confused by the lack of code and the image - what should this image tell me? We care about code, not about pictures. If you have a specific problem, consider studying [how-to-ask](https://stackoverflow.com/help/how-to-ask) and [on topic](https://stackoverflow.com/help/on-topic) , provide code respecting [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and your exception / expectation that do not get met by your code and I am sure SO will help you out. – Patrick Artner Feb 20 '18 at 10:36
  • Sebastian L I could not do anything. I tried sibling but nevethless it does not work – freedom667 Feb 20 '18 at 10:52

1 Answers1

1

You can get the index of a GameObject from its transform.GetSiblingIndex() ... you can also set it with transform.SetSiblingIndex(int index)

So I think what you are asking is how to update the index of an item in the list to the index of the item in the higharchy.

1st let me assume you are using UnityEngine.UI.Extensions.ReorderableList

2nd I am assuming your list is already populated with all the elements under the gameObject in question or at least the ones whoes position you care about.

now create a new MonoBehaviour or add a funciton to a new one as such.

public void UpdateSiblingIndex(ReorderableList.ReorderableListEventStruct eventData)
{
       eventData.DroppedObject.transform.SetSiblingIndex(eventData.ToIndex);
}

Next in your ReorderableList assign this function to the OnElementAdd, OnElementDrop and any other events from the list where you want to insure that element gets its sibling index updated.

James McGhee
  • 131
  • 4