简体   繁体   English

将预制件统一设置为另一个游戏 object 的孩子

[英]Setting a prefab as child of another game object in unity

 public GameObject aiArrow;
 aiArrow.transform.parent = this.gameObject.transform;

When I try to set up a prefab as a child of another object the following error occurs.当我尝试将预制件设置为另一个 object 的子级时,会发生以下错误。

Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption设置驻留在预制资产中的转换的父级被禁用以防止数据损坏

How to Set up this "aiArrow" Prefab as a child of another game object.如何将此“aiArrow”预制件设置为另一个游戏 object 的子项。

You can do this action right in the Instantiate() function call.您可以直接在Instantiate() function 调用中执行此操作。

public Transform parentObject;
public GameObject prefab;

public void CreateObject()
{
   Instantiate(prefab, parentObject);
}

The method Instantiate has several overloads, where you can specify a parent object. This was the simplest example. Instantiate方法有多个重载,您可以在其中指定父级 object。这是最简单的示例。 Important.重要的。 The example above does NOT modify position or rotation.上面的示例不修改 position 或旋转。 It only acts as you would drag&drop a gameobject in the Hierarchy below another one.它的作用就像您将Hierarchy中的一个游戏对象拖放到另一个游戏对象下方一样。 See Instatiate for the overload you need.请参阅Instatiate了解您需要的过载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM