Particle effect stop

Hi there,

I want to make a game where you have to destroy game objects, on a destroy you see a particle effect. it is starting on destroying but it won’t stop. This is my code:

 using UnityEngine;
using System.Collections;

public class RedEnemy : MonoBehaviour
{

    [SerializeField] private AudioSource _as;
    [SerializeField] private AudioClip _explosionSound;

    public GameObject FireworksAll;

    void Start()
    {

    }

    void Update()
    {

    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "player")
        {

            Destroy(gameObject);

            Explode();

            _as.PlayOneShot(_explosionSound);
        }
    }

    void Explode()
    {
        GameObject firework = Instantiate(FireworksAll, transform.position, Quaternion.identity);
        firework.GetComponent<ParticleSystem>().Play();
    }
}

Thanks Sam

Make the particle effect non-looping. Almost the first item in particle parameters, called Looping. Just disable it and your particle will play once.

1 Like

I’d like to give you a different look in case you wanted looping on.

void Explode()
    {
        GameObject firework = Instantiate(FireworksAll, transform.position, Quaternion.identity);
        firework.GetComponent<ParticleSystem>().Play();
StartCoRoutine(StopParticles(firework));
    }

IEnumertor StopParticles(GameObject firework)
{// Stops particles after 1 second
yield return new WaitForSeconds(1f);
firework.GetComponent<ParticleSystem>().Stop();
}
1 Like

Thanks, both helped!

1 Like