Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Encountering Null Reference Exception When trying to manipulate particle system during runtime

Discussion in 'Scripting' started by lifetree, Jan 26, 2019.

  1. lifetree

    lifetree

    Joined:
    Mar 28, 2014
    Posts:
    49
    Hi,

    As the title says, I am getting a null reference exception. Here is the full error text:

    "NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
    UnityEngine.ParticleSystem+MainModule.set_startColor (UnityEngine.ParticleSystem+MinMaxGradient value) (at /Users/builduser/buildslave/unity/build/artifacts/generated/bindings_old/common/ParticleSystem/ParticleSystemBindings.gen.cs:50)"

    I am trying to adjust the start color and reload the particle systems, and it works on the first screen when I hit play, but when I switch cameras (using Camera Transitions asset), it throws the error above. I am automatically instantiating a prefab of the particle system gameobject when I transition between cameras, and set the transform to the camera, so that it is always in front of the camera. No issues there, just letting you know the system so that it is easier to troubleshoot.

    Here is my code:
    Code (CSharp):
    1. void Awake()
    2. {
    3.  
    4. allParticleSystems = Resources.FindObjectsOfTypeAll<ParticleSystem>()
    5.  
    6. colorR = (float)0.3867925 * brightnessSlider.value;
    7. colorG = (float)0.2996167 * brightnessSlider.value;
    8. colorB = (float)0.1879228 * brightnessSlider.value;
    9. colorAlpha = (float)0.01176471 * brightnessSlider.value;
    10.  
    11. foreach (ParticleSystem p in allParticleSystems)
    12. {
    13. var main = p.main;
    14. main.startColor = new Color(colorR, colorG, colorB, colorAlpha);
    15. }
    16. }
    17.  
    18. void Update()
    19. {
    20. if (Input.GetMouseButtonUp(0) && brightnessBeingChanged == true)
    21. {
    22. brightnessBeingChanged = false;
    23.  
    24. //Set to bright green here so I can easily see the particles for troubleshooting.
    25. colorR = (float)0.0 * brightnessSlider.value;//(float)0.3867925 * brightnessSlider.value;
    26. colorG = (float)1.0 * brightnessSlider.value;//(float)0.2996167 * brightnessSlider.value;
    27. colorB = (float)0.0 * brightnessSlider.value;//(float)0.1879228 * brightnessSlider.value;
    28. colorAlpha = (float)1.0 * brightnessSlider.value;//(float)0.01176471 * brightnessSlider.value;
    29.  
    30. Debug.LogError(string.Format("Color profiles: ColorR={0}, ColorG={1}, ColorB={2}, ColorAlpha={3}", colorR, colorG, colorB, colorAlpha ));
    31.  
    32. reloadParticleSystem = true;
    33.  
    34. foreach (ParticleSystem p in allParticleSystems)
    35. {
    36. var main = p.main;
    37. main.startColor = new Color(colorR, colorG, colorB, colorAlpha);
    38. }
    39. }
    40. }
    I have the rest of the code in the script that is attached to each camera. It's function is to stop the particle system and start it again so that the particle color changes. I don't think it is relevant to this issue, but if you would like to see it, let me know and I will post it.

    My question is, why would it not be throwing the error when being called through the awake function, but it would throw it for each subsequent transition, under the Update function? The code block in question is identical to the one under Awake. I got the code from https://docs.unity3d.com/ScriptReference/ParticleSystem.MainModule-startColor.html. If this is out of date, then maybe that would be the issue, but in that case, I still don't know why it would be working for the very first camera when I hit play.

    If anyone can help, that would be most appreciated.

    Thanks!
     
  2. lifetree

    lifetree

    Joined:
    Mar 28, 2014
    Posts:
    49
    I should clarify that the above script is my GameManager script, so I only have one of those in the scene. When the brightness slider is changed, it executes the block of code in the Update method and sets reloadParticleSystem = true. As soon as that happens, the script on the active camera executes a block of code that reloads the particle system. But the error is with this section:
    Code (CSharp):
    1. var main = p.main;
    2. main.startColor = new Color(colorR, colorG, colorB, colorAlpha);
     
  3. lifetree

    lifetree

    Joined:
    Mar 28, 2014
    Posts:
    49
    I have just tried this and it also didn't work:
    Code (CSharp):
    1. private ParticleSystem.MainModule mainSystemModule;
    2.  
    3. void Update()
    4. {
    5. foreach (ParticleSystem p in allParticleSystems)
    6. {
    7. mainSystemModule = p.main;
    8. mainSystemModule.startColor = new Color(colorR, colorG, colorB, colorAlpha);
    9. }
    10. }
     
  4. lifetree

    lifetree

    Joined:
    Mar 28, 2014
    Posts:
    49
    Quick update: As I was troubleshooting, I realized that this is most likely related to the fact that I am instantiating new particle systems, and even though I tried updating the array whenever I instantiated a new particle system, it still didn't work. My next course of action will be to try without instantiating and instead have a particle system already in place for each camera, and I will enable and disable when necessary. That way I can have access to all particle systems that will ever be in the scene using Resources.FindObjectsOfTypeAll<ParticleSystem>(). I'm thinking that will fix this issue, but I can't test as I have an appt. I will let you know what I find as soon as I can test this.

    Thanks
     
  5. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    I'm not at home, but a quick look when you declare allParticleSystems shouldn't it be an array?

    AllParticleSystems[] =?
     
  6. lifetree

    lifetree

    Joined:
    Mar 28, 2014
    Posts:
    49
    I have the array declared at the top of the script as such:
    Code (CSharp):
    1. private ParticleSystem[] allParticleSystems;
    I was just trying to copy and paste the relevant info above so nobody has to slog through all the code to find the pertinent bits :)

    I am about to change and test as I mentioned above. I will let you know the results.
     
  7. lifetree

    lifetree

    Joined:
    Mar 28, 2014
    Posts:
    49
    After testing using a prefab object for each particle system as opposed to programmatically instantiating, it is working now. I am still getting the error, but it is functioning as I expect it to, so I'm not sure how concerned I should be about the error.

    So in general my issue was that I was trying to instantiate objects in my script and access them from my script, which was proving difficult. I'm sure someone more versed in C# and game dev in general could have figured out a solution (if you have one, I am more than happy to listen), but for now I will use this method since I have gotten it working and we are behind schedule as it is with this game. Hope this was able to help somebody.

    If it makes any difference, I did try reloading the array after instantiating so that the new object would be in the array, but that wasn't working for whatever reason. Maybe someone can figure it out.