gpt4 book ai didi

c# - 实例化并销毁 GameObject/ParticleSystem

转载 作者:行者123 更新时间:2023-12-02 00:38:16 28 4
gpt4 key购买 nike

我目前有一个脚本应该实例化一个粒子系统并在一定时间后销毁它,但是在实例化对象之后,出现此错误代码:

MissingReferenceException: The object of type 'ParticleSystem' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

脚本目前是这样的:

public class MuzzleFlash : MonoBehaviour {

public Transform gun;
public ParticleSystem smoke;
public ParticleSystem flare;
public ParticleSystem bullet;
private float smokeTime = 0.2f;

private void Update () {
if (Input.GetButtonDown("Fire1"))
{
smokeFun();
flare.Play();
bullet.Play();
}
}

void smokeFun()
{
Instantiate(smoke, gun);
Destroy(smoke, smokeTime);
}
}

我怎样才能实例化这个粒子系统并正确销毁它?

最佳答案

您正试图破坏 smoke 变量而不是您实例化的 ParticleSystem 预制体 ParticleSystem

Instantiate 函数返回它实例化的任何 GameObject。要销毁您刚刚实例化的 ParticleSystem,您必须对其进行实例化,将其存储在一个临时变量中,然后稍后使用该临时变量销毁它。

void smokeFun()
{
//Instantiate and store in a temporary variable
ParticleSystem smokeInstance = Instantiate(smoke, gun);
//Destroy the Instantiated ParticleSystem
Destroy(smokeInstance, smokeTime);
}

关于c# - 实例化并销毁 GameObject/ParticleSystem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48374606/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com