Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity C#4.5.2 2D销毁实例化粒子系统预制_C#_Unity3d_2d_Destroy_Particles - Fatal编程技术网

Unity C#4.5.2 2D销毁实例化粒子系统预制

Unity C#4.5.2 2D销毁实例化粒子系统预制,c#,unity3d,2d,destroy,particles,C#,Unity3d,2d,Destroy,Particles,我获得了这段代码片段,它实例化了一个粒子系统预置。我遇到的问题是克隆在5秒延迟后不会被摧毁。任何建议都将不胜感激 private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position) { ParticleSystem newParticleSystem = Instantiate( prefab, position, Quaternion.identity

我获得了这段代码片段,它实例化了一个粒子系统预置。我遇到的问题是克隆在5秒延迟后不会被摧毁。任何建议都将不胜感激

private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
{
    ParticleSystem newParticleSystem = Instantiate(
        prefab,
        position,
        Quaternion.identity
        ) as ParticleSystem;

    if(newParticleSystem.gameObject != null)
    {
        Destroy(
            newParticleSystem.gameObject,
            newParticleSystem.startLifetime
            );
    }

    return newParticleSystem;
}

您的代码依赖于所谓的ParticleSystem来跟踪何时销毁系统。我要做的是:

private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
{
    ParticleSystem newParticleSystem = Instantiate(
        prefab,
        position,
        Quaternion.identity
        ) as ParticleSystem;

    newParticalSystem.AddComponent<TimedDestroy>().delay = newParticleSystem.startLifetime;

    return newParticleSystem;
}
using UnityEngine;
public class TimedDestroy : MonoBehaviour
{
    public float delay;

    void Start()
    {
        Invoke("destruct",delay);
    }

    public void destruct()
    {
        Destroy(gameObject);
    }
}