Search Unity

Question Can't change Particle system simulation speed via script

Discussion in 'Editor & General Support' started by timurzbs, Sep 20, 2021.

  1. timurzbs

    timurzbs

    Joined:
    Jun 17, 2021
    Posts:
    7
    Hello everyone, today I encountered an error with particle system in unity, even simple code provided by unity documentation isn't working.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RainSlower : MonoBehaviour
    6. {
    7.     private ParticleSystem ps;
    8.    
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         ps = GetComponent<ParticleSystem>();      
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         var main = ps.main;
    19.         main.simulationSpeed = 0.1f;
    20.  
    21.     }
    22. }
    This code outputs an error in unity editor

    NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance

    I really don't understand what I'm doing wrong, please help!

    Also I'm using last recommended unity version 2020.3.18f1.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    It doesn't even remotely matter what you're doing. When you see this error, stop and fix it. Here's how:

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

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

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    timurzbs likes this.
  3. timurzbs

    timurzbs

    Joined:
    Jun 17, 2021
    Posts:
    7
    Sorry, but I don't think this is the case here, because I used the simplest code from the unity documentation, and watched different threads about issue like this https://forum.unity.com/threads/do-...t-them-from-a-particlesystem-instance.601489/ but the problem is that their solution only works in the inspector, when I build the game it still doesn't work. Sorry if I'm wrong about this.

    This is the code that doesn't output an error in the inspector, however it is still doesn't work when I build the game.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RainSlower : MonoBehaviour
    6. {
    7.     public ParticleSystem ps;
    8.     private GameManager man;
    9.  
    10.     public float rainSpeed = 0.1f;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         ParticleSystem ps = GetComponent<ParticleSystem>();
    15.         ParticleSystem.MainModule main = ps.main;
    16.  
    17.         man = FindObjectOfType<GameManager>();
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         var main = ps.main;
    25.         if(man.slowedTime == true)
    26.         {
    27.           main.simulationSpeed = rainSpeed;
    28.         } else
    29.         {
    30.             main.simulationSpeed = 1f;
    31.         }
    32.      
    33.     }
    34. }
     
    Last edited: Sep 20, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Whatever GameObject this script is running on (remember, that could be the wrong one too!), it is almost certain that an enabled ParticleSystem is NOT present at the time Start() gets executed.

    ALSO: in your second script you have a local variable called
    ps
    , so the class-level ps will ALWAYS be null. That will also cause problems. Go back to your FIRST script and get that working. It will work, but you MUST have an enabled ParticleSystem on that VERY SAME GameObject.
     
    timurzbs likes this.
  5. timurzbs

    timurzbs

    Joined:
    Jun 17, 2021
    Posts:
    7
    Yeah, it was the null reference error all along... Thank you for help, and sorry for your time, it is the good lesson for me!
    https://docs.unity3d.com/ScriptReference/ParticleSystem.MainModule-simulationSpeed.html I was confused because of documentation error.
     
    Kurt-Dekker likes this.