【问题标题】:Instantiate a gameobject as Child将游戏对象实例化为 Child
【发布时间】:2016-03-07 17:18:43
【问题描述】:

我正在使用 Unity 开发 2D UI 应用程序,但遇到了问题。 我正在编写脚本来实例化我的弹出窗口(我做了一个预制件)。我成功了,但后来 Unity 崩溃了,我不得不重做我的场景(忘了按 ctrl+s) 自这次崩溃以来,我的弹出窗口没有实例化为我的画布的子项,并且我收到了以下消息: “设置驻留在预制件中的转换的父级被禁用以防止数据损坏”

这是我的脚本:

using UnityEngine;
using System.Collections;

public class Popup : MonoBehaviour
{
    public RectTransform popupPrefab;
    private Animator anim;

    // Use this for initialization
    void Start()
    {
        //get the animator component
        anim = popupPrefab.GetComponent<Animator>();
        //disable it on start to stop it from playing the default animation
        anim.enabled = false;
    }

    public void CreatePopup()
    {
        // Copie du prefab
        RectTransform newPopup = Instantiate(popupPrefab, popupPrefab.transform.position, popupPrefab.transform.rotation) as RectTransform;
        newPopup.transform.SetParent(transform, false);

        //anim = newPopup.GetComponent<Animator>();
        //anim.enabled = true;
        //anim.Play("Popup");
    }

    public void ClosePopup()
    {
        anim.enabled = true;
        anim.Play("ClosePopup");
    }
}

我不明白为什么会出现这个错误,因为它在崩溃之前工作正常......

如果你有任何想法 谢谢

【问题讨论】:

  • 是的,很抱歉是 UI 对象,我用画布和 UI 工具创建了一个完整的 UI 界面
  • 这是我的实际代码你是什么意思?
  • 嗨@danegirard。请勾选一个答案以帮助保持董事会整洁。
  • 哦,是的,对不起,我从头开始重做我所有的项目,以使这个错误消失......

标签: c# unity3d instantiation


【解决方案1】:

我从头开始重新做我的项目解决了这个问题......

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    相关资源
    最近更新 更多