- Home /
How to Destroy Particle System after Instantiating,How to destroy a particle system that was just spawned?
I've tried Destroy() but it doesnt sem to work,I just instantiated a particle system, which I want to destroy. I've tried Destroy(Particle_System); doesn't work.
void Awake()
{
transformleftEngine = LeftEngine.GetComponent<Transform>();
transformrightEngine = RightEngine.GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
float scalarProduct = Vector2.Dot(transform.up, rb.velocity);
float forceFactor = -2 / Mathf.PI * Mathf.Atan(compressionFactor*scalarProduct) +1;
Force = initialForce * forceFactor;
if (Input.GetButtonDown("Engine"))
{
engines = true;
Instantiate(engineFXright, transformrightEngine);
Instantiate(engineFXleft, transformleftEngine);
}
if (Input.GetButtonUp("Engine"))
{
engines = false;
// This is where I want to destroy the particles
}
When you instantiate something, a clone is created to your scene, hence you need a Transform variable in your script to represent that clone if you want to manipulate it further such as destroying it.
But I need to also stress that instantiate a prefab then destroy its clone during Update risks memory leak. I assume you would constantly playing the same particle effect in thr same scene. If that’s the case you should ins$$anonymous$$d instantiate the prefab during Awake/Start/OnEnable to a Transform var and GetComponent of the particle system to a ParticleSystem var (uncheck “Play On Awake” of your particle effect prefab). During Update, clear and (re)play the particle system variable.
Answer by Trevdevs · Oct 01, 2018 at 01:04 AM
You need to add a reference, unity cannot just find the engineFXleft and right when you instantiate them. If you want to do it your way add references such as this.
// Above awake
private GameObject leftFX;
private GameObject rightFX;
//Change the following
rightFX = Instantiate(engineFXright, transformrightEngine) as GameObject;
leftFX = Instantiate(engineFXleft, transformleftEngine) as GameObject;
//Where you want to destroy
Destroy(leftFX);
Destroy(rightFX);
Personally, I don't see why you just stop the particle system and reactive it when you need it instead of instantiating over and over again which is tasking on the computer. If you want to try it this way follow this code
//Assign in inspector
public ParticleSystem leftFX;
public ParticleSystem rightFX;
void Awake()
{
transformleftEngine = LeftEngine.GetComponent<Transform>();
transformrightEngine = RightEngine.GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
float scalarProduct = Vector2.Dot(transform.up, rb.velocity);
float forceFactor = -2 / Mathf.PI * Mathf.Atan(compressionFactor * scalarProduct) + 1;
Force = initialForce * forceFactor;
if (Input.GetButtonDown("Engine"))
{
engines = true;
//Make sure they are not on play on awake
leftFX.Play();
rightFX.Play();
}
if (Input.GetButtonUp("Engine"))
{
engines = false;
leftFX.Stop();
rightFX.Stop();
}
}
I didn't test this as im not home but it worked in my head let me know if it doesn't :)
Thanks!! It works perfectly, all I had to add was a stop() in the awake function to make sure the engines start off, apart from that it works perfectly.