I want to create a script to toggle a particle emitter on/off every second, but I can't seem to get it to work properly. Here's what I have so far:
var emissionTime : float = 1.0;
var emissionDelay : float = 1.0;
function Start() {
TimedEmit();
}
function TimedEmit() {
while (1) {
particleEmitter.emit = true;
yield WaitForSeconds(emissionTime);
particleEmitter.emit = false;
yield WaitForSeconds(emissionDelay);
}
}
It almost works, except that it stops after 3 cycles (i.e. after turning on then off 3 times). What am I doing wrong?
The easiest way to do this is write a coroutine that yields for 1 second and then toggles the boolean
function MyCoroutine()
{
while(condition == true)
{
bool = !bool;
yield WaitForSeconds(1); // waits 1 second
}
}
The bool would be your emitter value. booleans can be toggled by setting it to be "not it's current value" since there are only 2 possible values.
The coroutine will continue on until that condition is no longer true...
If you don't understand coroutines I wrote a tutorial a while back here:
http://infiniteunity3d.com/2009/09/27/tutorial-coroutines-pt-1-waiting-for-input/
Hope that helps!
==
I figured out that it was because since the particles had a lifespan (energy) of 1, the particle system was automatically destroying itself after all the particles died. To fix it, I simply set the autodestruct property of the particle animator to false. For anyone interested, here's the fixed code:
var emissionTime : float = 1.0;
var emissionDelay : float = 1.0;
function Start() {
TimedEmit();
}
function TimedEmit() {
GetComponent(ParticleAnimator).autodestruct = false;
while (1) {
particleEmitter.emit = true;
yield WaitForSeconds(emissionTime);
particleEmitter.emit = false;
yield WaitForSeconds(emissionDelay);
}
}