如何在运行时控制实例化的预制件作为子级(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;

如何在运行时控制实例化的预制件作为子级(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;