Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Cant enable/disable particle emission

Discussion in 'Scripting' started by roger0, Jul 2, 2020.

  1. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,169
    I just want to enable/disable emission from a particle system but i get an error on the third line.

    Im doing everything by the book but it still errors out.


    ParticleSystem.EmissionModule em = smokeObject.emission;
    em.enabled = false;
    smokeObject.emission = em;

    Assets\packages\AirStrike\Scripts\WeaponSystem\Damage.cs(490,13): error CS0200: Property or indexer 'ParticleSystem.emission' cannot be assigned to -- it is read only

    I think I've been doing it this way without issues since they depreciated particlesystem.enableEmission but i'm running into issues now.
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    151
    You don't need this line (the error is there...)
    Code (CSharp):
    1. smokeObject.emission = em;
    The two first lines should do the job perfectly if all you want is enable/disable emission .
     
  3. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,169

    When I do that once I run the code it says.

    NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
     
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    151
    I did a quick test and there is no any Error. And it works fine...
    Code (CSharp):
    1.     public ParticleSystem ps;
    2.     public bool noEmission = false;
    3.    
    4.     private void Update()
    5.     {
    6.         ParticleSystem.EmissionModule emissionModule = ps.emission;
    7.  
    8.         if (noEmission)
    9.         {
    10.             emissionModule.enabled = false;
    11.         }
    12.         else
    13.         {
    14.             emissionModule.enabled = true;
    15.         }
    16.     }
    17.  
     
unityunity