Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

5.4 Particle System Error

Discussion in 'Scripting' started by CloudKid, Aug 24, 2016.

  1. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Hey Guys!
    I just updated to 5.4 and sometimes I get this strange error on my particle system:

    Code (CSharp):
    1. NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
    2. UnityEngine.ParticleSystem+EmissionModule.set_enabled (Boolean value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:362)
    This is caused by the following lines of code:
    Code (CSharp):
    1.         if (_smokeParticles == null)
    2.         {
    3.             _smokeParticles = new List<ParticleSystem>();
    4.             _smokeParticles.Add(_rearLeftWheel.GetComponentInChildren<ParticleSystem>());
    5.             _smokeParticles.Add(_rearRightWheel.GetComponentInChildren<ParticleSystem>());
    6.         }
    7.  
    8.  
    9.         foreach (var smoke in _smokeParticles)
    10.         {
    11.             var emision = smoke.emission;
    12.             emision.enabled = value;
    13.  
    14.         }
    15.     }
    emision.enabled = values is the thing that actually throws the error.

    Things to note:
    The error is not thrown every time I call the respective function
    This never happens before 5.4
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    285
    Is emision null on line 12?
     
  3. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    No, I get "UnityEngine.ParticleSystem+EmissionModule" when I debug the element in a try/catch loop:
    Code (CSharp):
    1.             var emision = smoke.emission;
    2.  
    3.             try
    4.             {
    5.                 emision.enabled = value;
    6.             }
    7.             catch (Exception)
    8.             {
    9.                 Debug.LogError(emision + " IS it null ?");
    10.             }
     
  4. jough

    jough

    Joined:
    Aug 18, 2013
    Posts:
    39
    I am encountering this same issue. @CloudKid were you ever able to find a solution?
     
  5. justdizzy

    justdizzy

    Joined:
    Mar 8, 2016
    Posts:
    39
    I was getting this because I was trying to use the variable before it was set. I instantiate a gameObject that has the particle system on it, and the script sets the variable in Start(), but I was calling a function of that script immediately after instantiation and not giving it a cycle to automatically call Start() as it should.
     
  6. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    29
    Field `Boost.BoostAvailableParticle' is never assigned to, and will always have its default value `null'
    Do not create your own module instances, get them from a ParticleSystem instance

    This warning and that error are what I get when trying to execute a piece of code, that is supposed to emit particles when a button is not on Cooldown.


    Code (CSharp):
    1. ParticleSystem BoostAvailableParticle;
    2. ParticleSystem.EmissionModule BoostAvailable;
    3.  
    4.     void Awake()
    5.     {
    6.         BoostAvailable = BoostAvailableParticle.emission;
    7.     }
    8.     void Update()
    9.     {
    10.         if (!OnCooldown)                                          
    11.         {
    12.             BoostAvailable.enabled = true;
    13.         } else if (OnCooldown)
    14.         {
    15.             BoostAvailable.enabled = false;
    16.         }
    What am I doing wrong here?
     
  7. justdizzy

    justdizzy

    Joined:
    Mar 8, 2016
    Posts:
    39
    If you are assigning the particle system in the editor, then you either need to add "[ SerializeField]" or "public" to it.
     
    Zythus likes this.
  8. richardzzzarnold

    richardzzzarnold

    Joined:
    Aug 2, 2012
    Posts:
    37
    I cannot believe how difficult it has become to use particle emission now....I have spent three days just trying to work out how to dynamically adjust emission rate ....without luck...
     
    Westland and JamesArndt like this.
  9. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    288
    Yeah, I'm getting similar problems - assigning a public particle system in the inspector, then caching the various modules I'm using from it (either in OnEnable/Awake/Start), before trying to access those cached modules in Update is giving me a "NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance"...
     
  10. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,464
    Has anyone explained or solved this yet? I have seen other threads, but the stuff is Greek to me. I don't understand in simple terms how to dynamically adjust the emission based on a dynamic value.
     
    tanoshimi likes this.
  11. skylinx

    skylinx

    Joined:
    Nov 1, 2012
    Posts:
    1
    Code (CSharp):
    1.  
    2.     //public particle system component visible in inspector
    3.     public ParticleSystem body1Particles;
    4.     //private emission module, null at this time
    5.     private ParticleSystem.EmissionModule body1ParticlesEmission;
    6.  
    7.     void Awake()
    8.     {
    9.         //set the private emission module to the public component emission module in awake
    10.         body1ParticlesEmission = body1Particles.emission;
    11.     }
    12.  
    13.     void Start
    14.     {
    15.         //acess the emission module using the new reference
    16.         //doing any actual write/read until after the reference is done at Awake
    17.         body1ParticlesEmission.enabled = false;
    18.     }
    19.  
    I'm using this for something to do with particle mesh scaling, but I use the emission module as well. The code above works perfectly for me as of 5.6. I've commented the code so hopefully it's not Greek

    Based on a dynamic value, code would look something like this

    Code (CSharp):
    1.  
    2.     public float emissionRate;
    3.     public ParticleSystem body1Particles;
    4.     private ParticleSystem.EmissionModule body1ParticlesEmission;
    5.  
    6.  
    7.     void Awake()
    8.     {
    9.         body1ParticlesEmission = body1Particles.emission;
    10.     }
    11.  
    12.     void Update
    13.     {
    14.         body1ParticlesEmission.rateOverTime = emissionRate;
    15.     }
    16.  
     
    Last edited: May 3, 2017
  12. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    299
    Place a break point and see that the EmissionModule is actually a struct. Usually those are passed by value like a Vector3, etc - not by reference

    However, according to this "Particle system modules do not need to be reassigned back to the system; they are interfaces and not independent objects."

    The error goes away when I do the precaution-check, varifying that the <ParticleSystem> component != null

    if all good, only then do I get the module and change its values.
     
    Last edited: Aug 30, 2017
    kreso likes this.
  13. LaserGreenAlien

    LaserGreenAlien

    Joined:
    Jun 14, 2018
    Posts:
    1
    Is there an answer yet?
     
  14. jquave

    jquave

    Joined:
    Oct 3, 2009
    Posts:
    21
    I had this issue and was able to solve it by moving any access to the particle system outside of Start() and Awake() functions. Apparently the engine needs 1 frame or so before these can be accessed.
     
  15. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    161
    >> SOLVED

    To avoid the errors you need to pass (or get) a reference to the GameObject containing the Particle System component. Then use the following pattern (noted in the Pause() and Stop() documentation, but not the emissionModule docs!)...

    Code (CSharp):
    1. {
    2.     get
    3.     {
    4.         if (_CachedSystem == null)
    5.             _CachedSystem = GetComponent<ParticleSystem>();
    6.         return _CachedSystem;
    7.     }
    8. }
    9.  
    10. private ParticleSystem _CachedSystem;
    This allows you to call Stop or Play... but they don't actually stop or play the system! I'm not sure how this can be, perhaps a Unity Shader dev can shed some light here. Feels like a bug, no?

    So I've found you have to go back to the emission pattern...

    Code (CSharp):
    1. void setBloodEmit (bool emit)
    2. {
    3.     if (BloodFxObject != null)
    4.     {
    5.         var emission = bloodFx.emission;
    6.         emission.enabled = emit;
    7.     }
    8. }
    Hope this helps others.

    <<

    >> UPDATE

    If I switch to Play() and Stop(), which seems to be the "right" way to do this. I get an error that says the that particle system doesn't exist anymore...

    Code (CSharp):
    1. MissingReferenceException: The variable _bloodFx of ActorMgr doesn't exist anymore.
    2. You probably need to reassign the _bloodFx variable of the 'ActorMgr' script in the inspector.
    Does Stop() kill the particle system? Maybe I want to use Pause()?

    <<

    I can't get this to work either.

    I've cached the emmisionModule in Awake() and checked to make sure that the "parent" particle system is != null.

    But, I still get the error noted above.

    I'm am trying to turn on emission via function call that occurs during Update().

    Code (CSharp):
    1. public void emitBloodFx (bool emit)
    2. {
    3.     if (_bloodFx != null)
    4.     {
    5.         _bloodFxEmission.enabled = emit;
    6.     }
    7. }

    Does this code, from the Unity Doc really work?

    https://docs.unity3d.com/2018.1/Doc...ce/ParticleSystem.EmissionModule-enabled.html

    Seems like it would throw the same error.

    Any advice would be appreciated!
     
    Last edited: Jul 25, 2018
  16. johnhupp

    johnhupp

    Joined:
    Sep 10, 2015
    Posts:
    1
    I came to this page via a search for the error message, and it seemed like everything in my code should have worked already. But I fixed one thing, and it magically started working:

    Before:
    Code (csharp):
    1. private ParticleSystem.EmissionModule emission;
    2. public float variableSetElsewhere;
    3.  
    4. void Start () {
    5.     emission = gameObject.GetComponent<ParticleSystem>().emission;
    6. }
    7.  
    8. void Update () {
    9.     emission.rateOverTime = new ParticleSystem.MinMaxCurve(variableSetElsewhere);
    10. }
    After:
    Code (csharp):
    1. private ParticleSystem.EmissionModule emission;
    2. public float variableSetElsewhere;
    3.  
    4. void Awake () {
    5.     emission = gameObject.GetComponent<ParticleSystem>().emission;
    6. }
    7.  
    8. void Update () {
    9.     emission.rateOverTime = new ParticleSystem.MinMaxCurve(variableSetElsewhere);
    10. }
    (Note that this code is dramatically simplified.)

    Do you see the difference? Yeah, apparently if you don't use the particleSystem variable inherited from MonoBehaviour (which didn't seem to work for me), you have to initialize it in Awake() rather than Start(), which is very much not obvious from the error message.