如何在运行时控制实例化的预制件作为子级(How to control instantiated prefabs at runtime as children)

编程入门 行业动态 更新时间:2024-10-25 12:24:18
如何在运行时控制实例化的预制件作为子级(How to control instantiated prefabs at runtime as children)

我在Unity中有一个游戏,每隔几秒就会实例化一次GameObjects。 然后在层次结构中,预制件显示在实例化的列表中,例如:

EnemyBlue(Clone) EnemyRed(Clone) EnemyGreen(Clone) EnemyBlue(Clone)

这堵塞了​​我的层次结构。 我的问题是,是否可以将GameObjects实例化为空GameObject的子节点,例如:

Enemies // An Empty GameObject EnemyBlue(Clone) EnemyRed(Clone) EnemyGreen(Clone) EnemyBlue(Clone)

I have a game in Unity where I instantiate GameObjects every few seconds. Then in the Hierarchy, the prefabs show up in a list of them being instantiated e.g.:

EnemyBlue(Clone) EnemyRed(Clone) EnemyGreen(Clone) EnemyBlue(Clone)

Which clogs up my hierarchy. My question is, is it possible to instantiate GameObjects as a child of an empty GameObject, for example:

Enemies // An Empty GameObject EnemyBlue(Clone) EnemyRed(Clone) EnemyGreen(Clone) EnemyBlue(Clone)

最满意答案

您可以设置所提及的ILiveForVR之类的转换的父级,但您也可以为已存在的内容设置父级:

gameObject.transform.parent = GameObject.Find("Name of game object").transform;

You can set the parent of a transform like ILiveForVR mentioned, but you can also set the parent for something that already exists:

gameObject.transform.parent = GameObject.Find("Name of game object").transform;

更多推荐

本文发布于:2023-08-07 20:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:预制件   实例   如何在   control   children

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!