Hey Guys! I just updated to 5.4 and sometimes I get this strange error on my particle system: Code (CSharp): NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance 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): if (_smokeParticles == null) { _smokeParticles = new List<ParticleSystem>(); _smokeParticles.Add(_rearLeftWheel.GetComponentInChildren<ParticleSystem>()); _smokeParticles.Add(_rearRightWheel.GetComponentInChildren<ParticleSystem>()); } foreach (var smoke in _smokeParticles) { var emision = smoke.emission; emision.enabled = value; } } 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
No, I get "UnityEngine.ParticleSystem+EmissionModule" when I debug the element in a try/catch loop: Code (CSharp): var emision = smoke.emission; try { emision.enabled = value; } catch (Exception) { Debug.LogError(emision + " IS it null ?"); }
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.
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): ParticleSystem BoostAvailableParticle; ParticleSystem.EmissionModule BoostAvailable; void Awake() { BoostAvailable = BoostAvailableParticle.emission; } void Update() { if (!OnCooldown) { BoostAvailable.enabled = true; } else if (OnCooldown) { BoostAvailable.enabled = false; } What am I doing wrong here?
If you are assigning the particle system in the editor, then you either need to add "[ SerializeField]" or "public" to it.
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...
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"...
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.
Code (CSharp): //public particle system component visible in inspector public ParticleSystem body1Particles; //private emission module, null at this time private ParticleSystem.EmissionModule body1ParticlesEmission; void Awake() { //set the private emission module to the public component emission module in awake body1ParticlesEmission = body1Particles.emission; } void Start { //acess the emission module using the new reference //doing any actual write/read until after the reference is done at Awake body1ParticlesEmission.enabled = false; } 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): public float emissionRate; public ParticleSystem body1Particles; private ParticleSystem.EmissionModule body1ParticlesEmission; void Awake() { body1ParticlesEmission = body1Particles.emission; } void Update { body1ParticlesEmission.rateOverTime = emissionRate; }
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.
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.
>> 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): ParticleSystem bloodFx { get { if (_CachedSystem == null) _CachedSystem = GetComponent<ParticleSystem>(); return _CachedSystem; } } 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): void setBloodEmit (bool emit) { if (BloodFxObject != null) { var emission = bloodFx.emission; emission.enabled = emit; } } 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): MissingReferenceException: The variable _bloodFx of ActorMgr doesn't exist anymore. 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): public void emitBloodFx (bool emit) { if (_bloodFx != null) { _bloodFxEmission.enabled = emit; } } 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!
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): private ParticleSystem.EmissionModule emission; public float variableSetElsewhere; void Start () { emission = gameObject.GetComponent<ParticleSystem>().emission; } void Update () { emission.rateOverTime = new ParticleSystem.MinMaxCurve(variableSetElsewhere); } After: Code (csharp): private ParticleSystem.EmissionModule emission; public float variableSetElsewhere; void Awake () { emission = gameObject.GetComponent<ParticleSystem>().emission; } void Update () { emission.rateOverTime = new ParticleSystem.MinMaxCurve(variableSetElsewhere); } (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.
Can any of you tell my how to update this code? I am so stuck! https://github.com/nickhall/Unity-Procedural/blob/master/Splines/Assets/Sources/LightningBolt.cs
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
Nope, using Awake does not fix the issue in Unit 2019.2.3f1 Here is my relevant code: Code (CSharp): private void Awake() { if (SwordFX != null) { m_chargingFX = SwordFX.GetComponent<ChargingFX>(); m_particleSystemModule = SwordFX.GetComponent<ParticleSystem>().main; m_particleSystemModule.startSize = 0.1f; } } 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.
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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tower : MonoBehaviour { [SerializeField] Transform objectToPan; [SerializeField] Transform targetEnemy; [SerializeField] float attackRange = 10; [SerializeField] ParticleSystem projectileParticle; // Update is called once per frame void Update() { if (targetEnemy) { FireAtEnemy(); } else { Shoot(false); } } private void FireAtEnemy() { float distanceToEnemy = Vector3.Distance(targetEnemy.transform.position, gameObject.transform.position); if (distanceToEnemy <= attackRange) { objectToPan.LookAt(targetEnemy); Shoot(true); } else { Shoot(false); } } private void Shoot(bool isActive) { ParticleSystem.EmissionModule emissionModule = projectileParticle.emission; emissionModule.enabled = isActive; } }