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

Unity Particle System, Emission Module, rate Over Time modify via script ...

Discussion in 'Scripting' started by Kasi2302, May 13, 2021.

Thread Status:
Not open for further replies.
  1. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Hello,

    Please help me.

    I've been dealing with this problem for about a week now, have read a lot and searched with google, but there are always compiler errors.

    So far I've been able to solve all the problems myself, but this time I just can't.

    I am using "Unity 2020.3.1f1" for my project, I am writing a script that should control the sequence in the current scene. The constant emission value "rateOverTime" should be changed with the script at a certain point in time. Unfortunately it doesn't work ...

    However, the script should not be attached to the Particle System, but is located in an empty game object.

    I even tried the example from the Unity documentation for the EmissionModule 2020.3, which also produces a compiler error.

    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour
    {
    private ParticleSystem ps;
    public float emissionrate = 1.0f;

    void Start()
    {
    ps = GetComponent<ParticleSystem>();
    }

    void Update()
    {
    var emission = ps.emission;
    emission.rateOverTime = emissionrate;
    }
    }

    Then the error comes ...

    NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
    UnityEngine.ParticleSystem+EmissionModule.set_rateOverTime (UnityEngine.ParticleSystem+MinMaxCurve value) (at <ac4201de60e7407889b38ca75ea7dc7e>:0)
    SceneMainController.Update () (at Assets/Scripts/SceneMainController.cs:44)

    I left the name of my Particle System as "Particle System", but I want to rename it to "vfx_fog".

    best regards
    Kasi

    Please take this into account in your answers.
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Kasi2302 likes this.
  3. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Kasi2302 likes this.
  4. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    First of all, thank you very much for the quick answer.

    But I've already tried it all in different versions and none of it works, unfortunately.

    Unity has changed the syntax / command structure a lot since the 2020 version. And that's why nothing that could be found on google worked so far, unfortunately.

    Unity may not have updated their documentation. :(
     
  5. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using TMPro;
    7.  
    8. public class SceneMainController : MonoBehaviour
    9. {
    10.  
    11.     float size = 0f;
    12.     float readsize;
    13.     public TMP_Text TextComponent;
    14.     private float timer = 0;
    15.     private float timerMax = 0;
    16.     private ParticleSystem ps;
    17.     public float emissionrate = 1.0f;
    18.  
    19.  
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         Screen.sleepTimeout = (int)0f;
    25.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
    26.  
    27.         readsize = TextComponent.fontSize;
    28.         TextComponent.fontSize = 0f;
    29.  
    30.         ps = GetComponent<ParticleSystem>();
    31.  
    32.  
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.        
    39.         if (!Waited(5)) return;
    40.  
    41.  
    42.         var emission = ps.emission;
    43.         emission.rateOverTime = emissionrate;
    44.  
    45.  
    46.         if (!Waited(5)) return;
    47.  
    48.         if (size < readsize)
    49.         {
    50.             size = size + 0.5f;
    51.             TextComponent.fontSize = size;
    52.         }
    53.  
    54.         else
    55.         {
    56.             return;
    57.         }
    58.  
    59.  
    60.     }
    61.  
    62.     private bool Waited(float seconds)
    63.     {
    64.         timerMax = seconds;
    65.  
    66.         timer += Time.deltaTime;
    67.  
    68.         if (timer >= timerMax)
    69.         {
    70.             return true; //max reached - waited x - seconds
    71.         }
    72.  
    73.         return false;
    74.     }
    75. }
    76.  
     
  6. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
    UnityEngine.ParticleSystem+EmissionModule.set_rateOverTime (UnityEngine.ParticleSystem+MinMaxCurve value) (at <ac4201de60e7407889b38ca75ea7dc7e>:0)

    Then the error message comes at the point where I want to address the emissions module.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Not sure where it's going wrong but the code above should NOT cause that special nullref error.

    That error comes from trying to new up your own module, rather than access the one in the particlesystem, which your code seems to do.

    Strip everything out, put this Start() function in a script, put it on a GO with a PS, and run:

    Code (csharp):
    1.     void Start ()
    2.     {
    3.         // make sure there IS a particle system on this GAmeObject!
    4.         ParticleSystem ps = GetComponent<ParticleSystem>();
    5.         float emissionrate = 1000f;
    6.         var emission = ps.emission;
    7.         emission.rateOverTime = emissionrate;
    8.     }
     
    Kasi2302 likes this.
  8. Kasi2302

    Kasi2302

    Joined:
    Sep 30, 2020
    Posts:
    25
    Many many thanks.
     
    Kurt-Dekker likes this.
  9. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Faced the same problem and... Wait what? Do I have to "ensure" this by calling "GetComponent()" every single time when I reference a particle system? Why it's not enough to do this only once in "Start()", for example? Why setting through the reference with inspector does not work?
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Yes indeed "Wait what?" You apparently completely misread the remainder of my post. Spicoli would be proud!

    It actually IS adequate to do it once in Start(), as I noted above and as my sample code shows.

    Source: I do it all the time, whenever I don't feel like using the inspector to drag stuff in.

    This works ALL DAY LONG for the ParticleSystem. You still must get the emission module in code.

    Source: I do it all the time, when I'm not using GetComponent<T>()
     
  11. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Then why topic starter got the error when he did exactly this: got ParticleSystem in Start() and then used local variables for referencing its module in Update()?
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Probably because he had another bug.

    For one, he named his class ExampleClass:

    For two, at the end of the day he certainly sounded happy after my post:

    Go ask Kasi! I can't read his mind.

    I stand by everything I wrote above.
     
  13. aydenchase2005

    aydenchase2005

    Joined:
    May 17, 2020
    Posts:
    2
    Wouldn't this only set "var emission" equal to whatever the particles already doing? It doesnt assign the variable back onto the particle system at any point there from the looks of things. How would I get ps.emission to become var emission after? there's no way it just 'knows' to use that variable
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Rather than asking cryptic "knows" questions against ancient posts that are looooong-ago closed...

    PLEASE start your own post if you have an issue.

    When you post, make it complete. We cannot read your mind. Here's a short checklist and how-to:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    Don't TALK about code. Copy/paste and POST the code, ONLY the relevant code, and then refer to it in your discussion.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
Thread Status:
Not open for further replies.