【问题标题】:Why do I get an error, when I instantiating prefab second time?当我第二次实例化预制件时,为什么会出现错误?
【发布时间】:2021-01-04 08:09:50
【问题描述】:

当我尝试向实例化的游戏对象添加预制件时,我收到错误消息:

禁止设置驻留在预制件中的转换的父级以防止数据损坏

当我第一次调用这个脚本时,一切正常。有很多类似的问题和答案。但是,我无法解决我的问题。

感谢您的帮助:)

GameObject coralWoodBundle = Resources.Load("Base/coral_wood_bundle") as GameObject;
GameObject woodlBundle = Instantiate(coralWoodBundle) as GameObject;
woodlBundle.transform.SetParent(this.gameObject.transform);

相关脚本

public class FallingWoodPackScript: MonoBehaviour
{
    public List<FallingWoodScript> fallingWoodScripts;

    public void Start()
    {
        RefillWood();
    }

    public void RefillWood()
    {
        GameObject coralWoodBundle = Resources.Load("Base/coral_wood_bundle") as GameObject;
        GameObject woodlBundle = Instantiate(coralWoodBundle) as GameObject;
        woodlBundle.transform.SetParent(this.gameObject.transform);
        woodlBundle.transform.localPosition = new Vector3(1f, 2.7f, 0);
        woodlBundle.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, -90));
        fallingWoodScripts = new List<FallingWoodScript>();
        foreach (Transform child in woodlBundle.transform)
        {
            fallingWoodScripts.Add(child.GetComponent<FallingWoodScript>());
        }
    }
}

【问题讨论】:

  • 你能发布整个脚本吗?
  • 我用脚本更新了描述。
  • 您正在使用的游戏对象是实例化的还是一直存在(甚至在运行模式之前)?
  • 它也是一个在播放模式下实例化的预制件。

标签: c# unity3d instantiation gameobject


【解决方案1】:

我很久以前就遇到过这个错误,我认为它的措辞有点误导。

也许你这样理解更好:

设置父 TO 驻留在预制件中的转换被禁用以防止数据损坏

我的猜测是 FallingWoodPackScript 参考 (=this) 你通过脚本调用 RefillWoodStart 实际上是一个预制参考,因此 this.gameObject.transform 是正如错误所说a transform which resides in a prefab

确保您仅在场景中的实际实例上调用RefillWood(或Start)。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2020-09-20
    • 2021-07-01
    • 2017-06-16
    • 2015-10-11
    • 1970-01-01
    • 2011-09-30
    • 2019-09-07
    • 2010-12-29
    相关资源
    最近更新 更多