- Home /
Turning a particle system on and off with collisions
What I want to do is attach 4 particle systems to a cube and have one running at a time. With each collision to a wall or object I want it to turn off the original particle system and turn on the next with script(java). The particle system is the flame preset found within the standard assets if you are curious. I figure the best way to go about it would be to turn on/off the game object, but not sure how to go about it with script. Any help is appreciated thanks. Joe
How about particleEmitter.emit = someBooleanWhichTellsItToBeOnOrOff;
Answer by syclamoth · Oct 16, 2011 at 04:28 AM
I'm answering this in C# because you asked for java, and it's the closest thing to that that Unity actually supports.
The way I would do it would be to have some kind of list which shuttles through the different particle emitters whenever you hit something.
// put this at the top along with the UnityEngine and System stuff.
// It provides us with the generic list functionality.
using System.Collections.Generic;
//Then, inside your class (you assign them in the editor)
public List<ParticleEmitter> emitters = new List<ParticleEmitter>();
int currentEmitter = 0;
// later, in your Collision bit
void OnCollisionEnter(Collision collision)
{
emitters[currentEmitter++].emit = false;
if(currentEmitter >= emitters.Count)
{
currentEmitter = 0;
}
emitters[currentEmitter].emit = true;
}
This way, every time you hit something, it switches to the next emitter! You just have to assign them in the editor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613011621im_/https://answers.unity.com/themes/thub/images/avi.jpg)