c# - 统一错误: Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption

标签 c# unity-game-engine

我正在尝试将以下对象实例化为该脚本所在的游戏对象的子对象:

 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/

相关文章:

c# - 尝试访问 WCF 服务时出现最新的 MonoTouch 4.0 问题

c# - 字符串比较的 DateTime.TryParseExact 方法

c# - 辅助功能自动化属性 "Name"是否需要本地化?

c# - 在 Unity 中使用触发器得分

php - 有没有办法将 UMA 头像配方**包括其转义字符**保存到我的数据库?

c# - 如何更改选项卡顺序或修改 winform 窗体中的选项卡页面集合?

c# - 执行文件复制/删除和移动之间的区别

c# - 在统一问题中展示二叉树

unity-game-engine - Unity网格有黑点

c# - 为什么在统一使用 Pixelperfect 相机时无法更改相机视口(viewport)矩形?