Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by VileGoo, Feb 8, 2020.

  1. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    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?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
  3. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    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

    Code (CSharp):
    1. ParticleSystem yourParticle;
    2. yourParticle.enableEmission = false;   /// directly turns off emission in the particle editor
    3. ...
    4. var yourParticleEmission = yourParticle.emission;
    5. ...
    6. yourParticleEmission.enabled = true;
    7. ...
    8.  
    9. yourParticleEmission.rateOverTime = yourEmissionRate;
    10.  
    11. // or for a funky light heavy effect:
    12.  
    13. yourParticleEmission.rateOverTime = Mathf.Lerp(particleEmissionMin, particleEmissionMax, startOfEmission/ emissionLength);
    14.  
    15.  
     
    Last edited: Feb 16, 2020
    lovewessman and xBoris like this.
  4. Nukee_

    Nukee_

    Joined:
    Mar 13, 2020
    Posts:
    1
    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...
     
  5. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    It's a ParticleSystem variable. ;)
     
  6. wlad_s

    wlad_s

    Joined:
    Feb 18, 2011
    Posts:
    148
    I wonder if this is expensive?

    Code (CSharp):
    1.  
    2. for(int i = 0; i < myParticleSystems.Length; i++) {
    3.       var emission = myParticleSystems[i].emission;
    4.       emission.rateOverTime = myEmissionRate;
    5. }
    6.  

    or should I store the emission modules in an array on initialization and then iterate through them instead?
     
    angelonit likes this.
  7. immortalssanctuary

    immortalssanctuary

    Joined:
    Jun 22, 2019
    Posts:
    1
    using static UnityEngine.ParticleSystem;
     
  8. angelonit

    angelonit

    Joined:
    Mar 5, 2013
    Posts:
    40
    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;
     
    Siliko likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    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. :)
     
  10. Noiky

    Noiky

    Joined:
    Aug 20, 2020
    Posts:
    1
    when you don't know the type of a variable you can just "print(Things.GetType())"