Skip to content

[Unity] Use SetSiblingIndex() To Set the Order of Child Game Object

Today I want to record how to use SetSiblingIndex() function to set the order of child game object under the parent game object in Unity.

This seems to be a less important feature. However, when I was generating a large number of objects today, I encountered a strange problem: sometimes the earlier generated object would cover the later generated object; sometimes the later generated object would cover the earlier generated object.

As a result, I could not design the scene well at all.

So finally I used the SetSiblingIndex() function to set the respective order, which solved the problem.


How to use SetSiblingIndex() function

Assume the objectList is a List<GameObject> data type object, we place the child game objects we generated, then we can use the following command to set their order (but if there are other objects under the parent object, you need to count the total number of child objects).

for (int i=0; i<objectList.Count; ++i) {
    objectList[i].gameObject.transform.SetSiblingIndex(i);
}




References


Read More

Tags:

Leave a Reply