How the heck do I access the Particle System Emission RateOverTime?

I’m getting really confused on the Emission Module, I don’t understand how to change it realtime.

public ParticleSystem whiteParticle;
private bool increaseWhiteRate = false;
public float whiteRateStart;
public float whiteRateTarget;
public float whiteRateIncreaseRate;

    if (increaseWhiteRate)
    {
        var emission = whiteParticle.emission;
        emission.rateOverTime = Mathf.MoveTowards(whiteRateStart, whiteRateTarget, whiteRateIncreaseRate * Time.deltaTime);
     
    }

As far as I can tell I’m writing what is in the documentation:

But it’s very clear I do not understand what I’m doing. Nothing changes in the script, the particles just generate at whatever I’ve edited to in the inspector, and they don’t seem affected by script at all.

Everything is hooked up correctly btw, thank you!

Its a struct. Structs are by value, meaning they get copied when you pass them around, so you need to assign it back.

var emission = whiteParticle.emission;
emission.rateOverTime = Mathf.MoveTowards(whiteRateStart, whiteRateTarget, whiteRateIncreaseRate * Time.deltaTime);
whiteParticle.emission = emission;

The differences in behaviour in value types and reference types is a critically fundamental C# thing to know:

1 Like

This is generally good C# advice, but, for historical reasons, assigning the struct back isn’t actually necessary, and has no effect, when dealing with particle system modules. so this suggestion won’t fix the issue. The particle system module properties are actually a direct interface into the engine. (I know this is not intuitive, and we agree it is the wrong design)

I don’t think there is enough info in the original post to diagnose the issue, but, i would suggest replacing the Mathf.MoveTowards code with a simple public float, which you can set to 0 then 10000, in the inspector, in playmode, and see if it’s having an effect on your system emission rate.

If it is, then try to figure out what’s wrong with that more complicated logic.

1 Like