我正在尝试将以下对象实例化为该脚本所在的游戏对象
的子对象:
var newEnergyPart = Instantiate<GameObject>(energyPart, energyBarLayout.transform.position, Quaternion.identity);
newEnergyPart.transform.SetParent(energyBarLayout.gameObject.transform);
但是,它一直给我这个错误:
Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'BarUnit(Clone)'). UnityEngine.Transform:SetParent(Transform)
我认为确保将父对象设置为设置为 n 实例化 gameobject
的对象将使错误消失,而它仍然出现。
最佳答案
它看起来像附加到 Prefab 对象的“energyBarLayout”脚本,您无法将“Prefab”设置为实例化变换的父级,因此应该检查并更改“energyBarLayout”。
就您而言,您尝试在实例化父对象之前创建“newEnergyPart”,因此我认为您应该首先实例化“energyBarLayout”GameObject,然后创建“newEnergyPart”,这样它肯定会起作用。
<
关于c# - 统一错误: Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583572/