Search Unity

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:
    419
    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. JoeTheGG

    JoeTheGG

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

    justdizzy

    Joined:
    Mar 8, 2016
    Posts:
    89
    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.
     
    Deleted User and Hoorza like this.
  6. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    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?
     
    D27 likes this.
  7. justdizzy

    justdizzy

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

    richardzzzarnold

    Joined:
    Aug 2, 2012
    Posts:
    140
    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...
     
    clinton_d, Westland and JamesArndt like this.
  9. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
    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,932
    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:
    3
    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:
    393
    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
    D27 and kreso like 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:
    238
    >> 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. ParticleSystem bloodFx
    2. {
    3.     get
    4.     {
    5.         if (_CachedSystem == null)
    6.             _CachedSystem = GetComponent<ParticleSystem>();
    7.         return _CachedSystem;
    8.     }
    9. }
    10.  
    11. 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. elsiehupp

    elsiehupp

    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.
     
    unity_9JxPXL3xiCMKmQ and BIO_K like this.
  17. BIO_K

    BIO_K

    Joined:
    Feb 3, 2016
    Posts:
    23
  18. johnsolo

    johnsolo

    Joined:
    Jan 12, 2014
    Posts:
    7
    I fixed this because my particleSystem was null, even though I thought for sure it wasn't. It ended up my script was attached to the wrong gameobject
     
  19. Deleted User

    Deleted User

    Guest

    Use the Awake() function instead of Start()
     
  20. FurionTheGreatest

    FurionTheGreatest

    Joined:
    Jun 12, 2017
    Posts:
    9
    Work in my case, ty
     
  21. OrderOfOut

    OrderOfOut

    Joined:
    Sep 21, 2018
    Posts:
    37
    Nope, using Awake does not fix the issue in Unit 2019.2.3f1 Here is my relevant code:
    Code (CSharp):
    1.  private void Awake()
    2.     {
    3.         if (SwordFX != null)
    4.         {
    5.             m_chargingFX = SwordFX.GetComponent<ChargingFX>();
    6.             m_particleSystemModule = SwordFX.GetComponent<ParticleSystem>().main;
    7.          m_particleSystemModule.startSize = 0.1f;
    8. }
    9. }
    10.  
    I still get "NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance" at the exact line I set the startSize. I'm thinking this is a bug, because I know the particle system is there and I even see it turning on, I just can't modify its properties.
     
  22. MarcusJT

    MarcusJT

    Joined:
    Oct 3, 2019
    Posts:
    4
    My error was that I forgot to reference "Projectile Particle" in the Inspector by dragging the particle system from the hierarchy into it.

    Hope this helps.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tower : MonoBehaviour
    6. {
    7.     [SerializeField] Transform objectToPan;
    8.     [SerializeField] Transform targetEnemy;
    9.     [SerializeField] float attackRange = 10;
    10.     [SerializeField] ParticleSystem projectileParticle;
    11.  
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (targetEnemy)
    17.         {
    18.             FireAtEnemy();
    19.         }
    20.         else
    21.         {
    22.             Shoot(false);
    23.         }
    24.     }
    25.  
    26.     private void FireAtEnemy()
    27.     {
    28.         float distanceToEnemy = Vector3.Distance(targetEnemy.transform.position, gameObject.transform.position);
    29.  
    30.         if (distanceToEnemy <= attackRange)
    31.         {
    32.             objectToPan.LookAt(targetEnemy);
    33.             Shoot(true);
    34.         }
    35.         else
    36.         {
    37.             Shoot(false);
    38.         }
    39.     }
    40.  
    41.     private void Shoot(bool isActive)
    42.     {
    43.         ParticleSystem.EmissionModule emissionModule = projectileParticle.emission;
    44.         emissionModule.enabled = isActive;
    45.     }
    46. }
    47.  
     
    Last edited: Apr 28, 2020