How Play and Stop a ParticleSystem through Scripting?

Hey guys, I’m trying turn on/off a simple particle emission, when i’m using Play()/Pause() …works fine(but particles many remains there paused), and what I want to do is when the I pause the emission, the game object stop generating particles and that particles already generated finish their cycles, like the Stop()…but when I use Play()/Stop(), the emission never starts…what should I do…? Here is my simple code:
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
	
	public bool x;
	public ParticleSystem dust;
	
	void Start () {
	
	}
	
	
	void Update () 
	{
		if(x){
			
			dust.Play();
		}
	
		else if(!x){
			
			dust.Stop();
			
			//dust.Pause();
			//dust.Clear();
		}
	}
}

Simply change ParticleSystem.enableEmission between true / false.

Well what you have there looks like it should work, other than the fact that you’re not defining the bool as true or false anywhere, so you can try this.

public class Test : MonoBehaviour 
{
	protected bool letPlay = true;
	
	public void Update()
	{
		if(Input.GetKeyDown(KeyCode.E))
		{
			letPlay = !letPlay;
		}
	
		if(letPlay)
		{
			if(!gameObject.particleSystem.isPlaying)
			{
				gameObject.particleSystem.Play();
			}
		}else{
			if(gameObject.particleSystem.isPlaying)
			{
				gameObject.particleSystem.Stop();
			}
		}
	}
}

I had the same problem, it worked for me but it seems to only work if i first check .isplaying or !.isplaying first, and then inside that block i use .Play or .Stop

At first I have faced the same problem, and now i have solved it!
First, set Looping = true
Second, set play on awake = false
Finally, in your code write:
void play()
{
ParticleSystem1.Play();
ParticleSystem1.enableEmission = true;
}
void stop()
{
ParticleSystem1.enableEmission = false;
}
It could work well!!!

I just add particleSystem.enableEmission = true right after particleSystem.Play() sentence, then it’s working!

using UnityEngine;
using System.Collections;

public class Thrust : MonoBehaviour
{
public bool letPlay = false;
public GameObject thrust1;
public GameObject thrust2;

void Start()
{
	thrust1.GetComponent<ParticleSystem>().Stop();
	thrust2.GetComponent<ParticleSystem>().Stop();
}

void Update()
 {
 
     if(letPlay)
     {
        thrust1.GetComponent<ParticleSystem>().Play();
        thrust2.GetComponent<ParticleSystem>().Play();
     }
     
	 else
	 {
		thrust1.GetComponent<ParticleSystem>().Stop();
		thrust2.GetComponent<ParticleSystem>().Stop();
     }
    
 }
 
 public void TrustOn()
 {
	letPlay = true;
 }
 
  public void TrustOff()
 {
	letPlay = false;
 }

}

//Ahi esta la Solucion Creas un boton con event Trigger en OnClicDown le pones el ThrustOn y en el OnClicUp le pones el TrustOff

Kind of a hacky way of doing it but I just do this and it works good:

public GameObject muzzleFlash;

void Update () {

if(Input.GetMouseButtonDown (0)) {
    muzzleFlash.SetActive(false);
    muzzleFlash.SetActive(true);

}

What a pain in the butt. Here’s a simple script that would turn on/off your Particle Effect when you hit the “H” button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleTest : MonoBehaviour {

	public GameObject myParticles;

    void Start(){
		myParticles.SetActive (false);
    }

    void Update(){
	if (Input.GetKeyDown ("h") && myParticles.activeSelf == false) {
		myParticles.SetActive (true);
	} else if (Input.GetKeyDown ("h") && myParticles.activeSelf == true) {
		myParticles.SetActive (false);
	}
}

This is attached to my Player, then you drag the ParticleSystem prefab you want to use in for your GameObject.

Make sure your ParticleSystem is set to PlayOnAwake* = true. This killed me for the longest time… we set a static reference and turn it on off first… then on and off from there.

Looping is whether you want it to fire once, or keep firing until you turn it off. This example is a campfire that turns on (Looping = true), and then turns back off when you hit the button again.

There’s probably a better way, but good luck finding it. Everything I’ve tried to use from Unity Manual & tutorials on using ParticleSystems doesn’t ever work for me.

In Unity3D editor 2020.3 and later (maybe before, dont know) the “particleSystem.enableEmission” is obsolete.
Use “ParticleSystem.EmissionModule.enabled”

        ParticleSystem.EmissionModule emission = this.particleSystem.emission;
        emission.enabled = playing;