- Home /
Toggle particle system on or off
I'm trying to switch the particle system on and off with code. The programme starts with no rain / particles then is switched on or off by the player. The way I thought of doing it would be to turn emissions off so that the particles just peter out. The scripting reference link text
won't work. It seems because I'm declaring the particle system in Start() and switching it on or off in Update()
void Start()
{
rainOff = GameObject.Find("sunCloud").GetComponent<SpriteRenderer>();
toggleRain = GameObject.Find("rainCloud").GetComponent<SpriteRenderer>();
toggleRain.enabled = false;
ParticleSystem ps = GetComponent<ParticleSystem>();
var em = ps.emission;
em.enabled = false;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 pos = Input.mousePosition;
Collider2D hitcollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));
if (hitcollider != null && (hitcollider.CompareTag("cloud")))
{
if (!toggleRain.enabled)
{
toggleRain.enabled = true;
rainOff.enabled = false;
GetComponent<AudioSource>().Play();
//-some code here relating to the emissions- = true;
}
else
{
toggleRain.enabled = false;
rainOff.enabled = true;
GetComponent<AudioSource>().Pause();
// -some code here relating to the emissions- = fa;se;
}
}
}
}
Answer by $$anonymous$$ · Jun 21, 2019 at 02:37 AM
I think first you should declare your ParticleSystem (ps) variable outside the Start() method so you would have access to it from other parts of the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DashboardUI : MonoBehaviour {
public ParticleSystem ps;
void Start() {
rainOff = GameObject.Find("sunCloud").GetComponent<SpriteRenderer>();
toggleRain = GameObject.Find("rainCloud").GetComponent<SpriteRenderer>();
toggleRain.enabled = false;
// Store the ParticleSystem component in the ps variable
ps = GetComponent<ParticleSystem>();
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
Vector3 pos = Input.mousePosition;
Collider2D hitcollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));
if (hitcollider != null && (hitcollider.CompareTag("cloud"))) {
if (!toggleRain.enabled) {
toggleRain.enabled = true;
rainOff.enabled = false;
GetComponent<AudioSource>().Play();
// Start the emission
ps.Play();
} else {
toggleRain.enabled = false;
rainOff.enabled = true;
GetComponent<AudioSource>().Pause();
// Stop the emission
ps.Play();
}
}
}
}
}
You can use
ps.Play();
To start emitting your particles, and
ps.Pause();
To stop emitting them.
Description of the ParticleSystem.Play() method
Starts the Particle System.
Sets the Particle Systems into play mode and enables emitting (if it has been disabled).
If the Particle System has been paused, then this resumes playing from the previous time. If the Particle System has stopped, then the system starts from time 0, and, if it is relevant, the startDelay is applied.
Link to the reference: https://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html
Answer by Freesmith · Jun 21, 2019 at 02:11 AM
hi, i think you are missing this line
em.enabled = true; // where you want to start emission
or em.enabled = false;
where you want to stop it
Your answer
Follow this Question
Related Questions
Particle system radial velocity 1 Answer
Add collision planes to Shuriken ParticleSystem prefabs 1 Answer
Does the Shuriken Particle System in Unity 4 not allow particles to render prior to collision? 1 Answer
Particle System Values Don't affect 0 Answers
Particles in Orthographic Camera Disappearing Too Soon 1 Answer