如何销毁实例化对象 Unity C#

本文关键字:Unity 对象 实例化 何销毁 | 更新日期: 2023-09-27 18:32:10

using UnityEngine;
using System.Collections;
public class myscript : MonoBehaviour {
    public Transform platform;
    public float z = 0.0f;
    void Update () 
    {
        float c_z=0.0f;
        Instantiate(platform,new Vector3(z++,0,0),Quaternion.identity);
        Camera.main.transform.Translate (0,0,c_z=c_z+0.1f);
    }
}

在上面的代码中,我的平台被实例化,当我的相机向前移动时,我必须摧毁我的平台的尾部。如何?

如何销毁实例化对象 Unity C#

尝试:

using UnityEngine;
using System.Collections;
public class myscript : MonoBehaviour {
    public Transform platform;
    public float z = 0.0f;
    private GameObject _obj;
    void Update () 
    {
        float c_z=0.0f;
        _obj = Instantiate(platform,new Vector3(z++,0,0),Quaternion.identity) as GameObject;
        Camera.main.transform.Translate (0,0,c_z=c_z+0.1f);
    }
}

然后,只要您准备好摧毁平台,就拨打Destroy(obj)