Turning all particle Emitters off and On

I’m hoping to make a hot key turn off/on all of particle emitters in my scene. I’ve been working with the FindObjectsofType function…but (as usual) I must be doing something wrong here.

My code:

var buttonName = "Smoke Toggle";
private var emitting : boolean = false;
private var emitted : boolean = false;



//check for input only if we aren't in the middle of emitting
function Update() {
   if (!emitting  Input.GetButtonDown(buttonName)) {
      if (emitted)
         EmitOn();
      else
         EmitOff();
   }
}

//turning emitter on
function EmitOn() {
	emitting = true;
		
	smoke = FindObjectsOfType (ParticleEmitter);
	smoke.particleEmitter.emit = true;
	
	smoke.emit = true;


   emitting = false;
   emitted = false;
}


//turning emitter off
function EmitOff() {
	emitting = true;
	
	smoke = FindObjectsOfType (ParticleEmitter);
	smoke.particleEmitter.emit = false;

	smoke.emit = false;

   emitting = false;
   emitted = true;
}

But, I get “MissingFieldException: Cannot find variable particleEmitter”

The documentation shows a much more complex list of code, but I’m unsure how to use it effectively here. The documentation lists:

// When clicking on the object, it will disable all springs on all
// hinges in the scene.
function OnMouseDown () {
hinges = FindObjectsOfType (HingeJoint);
for (var hinge : HingeJoint in hinges) {
hinge.useSpring = false;
}
}

How am I doing that wrong?

Try this:

var buttonName = "Smoke Toggle";
private var emitting = true;

function Update ()
{
	if (Input.GetButtonDown (buttonName))
	{
		if (emitting)
			ParticlesEmit (false);
		else
			ParticlesEmit (true);
	}
}

function ParticlesEmit (index : boolean)
{
	var particleSystems : ParticleEmitter[];
	particleSystems = FindObjectsOfType (ParticleEmitter);
	if (index)
	{
		for (i = 0; i < particleSystems.length; i++)
			particleSystems[i].particleEmitter.emit = true;
		
		emitting = true;
	}
	else
	{
		for (i = 0; i < particleSystems.length; i++)
			particleSystems[i].particleEmitter.emit = false;
		
		emitting = false;
	}
}

Holy cow! Thats cool…

So very far above my head, it reminds me how much more I’ve got to learn [sigh]. But man, it does exactly the thing I needed it to do.

Thanks much!

No offence to Daniel (obviously this code does what you want perfectly well), but these changes make the code shorter and easier to understand:

var buttonName = "Smoke Toggle";
private var emitting = true;

function Update() {
	if (Input.GetButtonDown(buttonName))
		ToggleAllEmitters(!emitting); //pass the opposite of the current state
}

function ToggleAllEmitters(emit : boolean) {
	//find all particle emitters
	var particleSystems = FindObjectsOfType(ParticleEmitter);
	//set them all to the passed state
	for (var particleSystem : ParticleEmitter in particleSystems)
		particleSystem.emit = emit;
	//record the state so we know what to do next time
	emitting = emit;
}

By the way, the reason your code was throwing an exception is because FindObjectsOfType() returns an array (a list of objects). In order to interact with the objects in the list rather than the list itself, you need to iterate through it using something like the for loop above. The notation I’ve used is called a “foreach” loop (even though JavaScript still uses the for keyword), and it’s designed for exactly this purpose. Daniel’s loop is the more general for loop which uses an explicit counter (“i”, in this case) and is more versatile than foreach.

Cool! Thanks for the code and explanation…