How do you change a particle systems emission Rate over Time in script?

I’ve looked up forums and checked the scripting API but I just can’t figure out how to actually change a particle systems emission rate over time. All I want to do is get the referenced ParticleSystem’s emission module and change the Rate over Time. Why is this so hard to do?

Does it not work if you use this? Unity - Scripting API: ParticleSystem.EmissionModule.rateOverTime

A lot of particle control in google searches is deprecated - I think some Unity docs maybe are as well, but they may be updated now … ?

You access through rateOverTime

ParticleSystem yourParticle;
yourParticle.enableEmission = false;   /// directly turns off emission in the particle editor
...
var yourParticleEmission = yourParticle.emission;
...
yourParticleEmission.enabled = true;
...

yourParticleEmission.rateOverTime = yourEmissionRate;

// or for a funky light heavy effect:

yourParticleEmission.rateOverTime = Mathf.Lerp(particleEmissionMin, particleEmissionMax, startOfEmission/ emissionLength);
3 Likes

Hi! first of all, thank you for this but I was wondering whats the variable type of this is?

var yourParticleEmission = yourParticle.emission;

It says it is an EmissionModule type but I can’t find anything related to that…

It’s a ParticleSystem variable. :wink:

I wonder if this is expensive?

for(int i = 0; i < myParticleSystems.Length; i++) {
      var emission = myParticleSystems[i].emission;
      emission.rateOverTime = myEmissionRate;
}

or should I store the emission modules in an array on initialization and then iterate through them instead?

1 Like

using static UnityEngine.ParticleSystem;

LOL for some reason this works no problem:

var emission = myParticleSystem.emission;
emission.rateOverTime = myEmissionRate;

But this doesn’t even compile:

myParticleSystem.emission.rateOverTime = myEmissionRate;

4 Likes

If you would like the gory details of why, google up “c# properties returning structs”

Otherwise, when in doubt see the documentation, which shows you exactly the correct way. :slight_smile:

when you don’t know the type of a variable you can just “print(Things.GetType())”