Search code examples
c#unity-game-engineparticle-system

Unity 5.3 Setting Particle System Emission Rate not changing


I checked the documentation which I found nothing to show how to change of my Emission Rate in my Particle System and I checked Reddit and found nothing.

This is what I am trying to change : enter image description here

The code I am using that I thought would work is this :

public ParticleSystem Smoke;


void Start()
{
    // Get the particle system (Smoke) Module.
    em = Smoke.emission;
    rate = em.rate;
    // Set the Mode to Constant.
    rate.mode = ParticleSystemCurveMode.Constant;

}

void Update()
{
    if (distance < 1f)
    {
        // Attempt to set the constant
        rate.constantMin = 20f;
        rate.constantMax = 20f;

    }
}

But with this above code when I look at my GameObjects Particle Systems Emission in the scene view and in the inspector nothing changes. What am I doing wrong?


Solution

  • This is a bit bulky in 5.3. You have to get the rate and store it in a local variable, change the values you want and then set it.

    void Update()
    {
        if(distance < 1f)
        {
            rate = em.rate;
            rate.constantMin = 20f;
            rate.constantMax = 20f;
            em.rate = rate;
        }
    }