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

    }

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 :slight_smile: