Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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,208
    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:
    257
    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,208

    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:
    257
    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.