如何销毁和实例化GameObject?

时间:2019-04-15 07:27:21

标签: c# unity3d

我有一个想要消灭并实例化的GameObject,具体取决于单击的按钮。我不太了解它是如何工作的,但是它类似于下面的代码吗?然后,我将该脚本附加到某个游戏对象上,然后通过函数名称将onClick设置。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreateDestroyAR : MonoBehaviour {

    //PAGE HAS BEEN CREATED AS A PREFAB
    public GameObject Page;
    public void CreatePage() {
        Instantiate(Page);
    }

    public void DestroyPage()
    {
        Destroy(Page);
    }
}

是的,它们是“页面”,我只能用setActive来代替,但是当单击不同的按钮时,该“页面”之一需要销毁,并在每次单击其相应按钮时重新实例化。

1 个答案:

答案 0 :(得分:0)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreateDestroyAR : MonoBehaviour {

    //PAGE HAS BEEN CREATED AS A PREFAB
    public GameObject Page;
    private GameObject instantiatedPage;
    public void CreatePage() {
       instantiatedPage = Instantiate(Page);
    }

    public void DestroyPage()
    {
        Destroy(instantiatedPage);
    }
}