Unity add child to children, but at top

随声附和 提交于 2019-12-10 12:53:27

问题


I am trying to add a child object to a collection of children, but I want to make sure the the latest will be the first.

Here is what I am trying to do:

GameObject
- (My new object here)
- GameObject
- GameObject
- GameObject

Here is the code I am using to instantiate my prefab:

GameObject messageObj = Instantiate(storyPrefab) as GameObject;
messageObj.name = "Story";
messageObj.transform.parent = wallGrid.transform;
messageObj.transform.localScale = new Vector3(1,1,1);

Hope this makes sense.

Any help is apreciated and thanks in advance :-)


回答1:


You can change the order in the hierarchy using Transform.SetSiblingIndex.

So basically you'd want to set the first index to your desired element:

messageObj.transform.SetSiblingIndex(0);



回答2:


There is a dedicated method:

transform.SetAsFirstSibling();


来源:https://stackoverflow.com/questions/36106736/unity-add-child-to-children-but-at-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!