Search Unity

Resolved How to use ParticleSystem.emission.enabled

Discussion in 'Scripting' started by Hexolenn, May 22, 2023.

  1. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I have been trying to find the best way to disable my particles and the best way I found was to stop the emission (Doesn't destroy already created particles). But disabling from the code proved to be tricky.

    I have looked at other peoples problems before posting it but either I cannot merge it correctly or it is out of date so I wanted ask here

    Code (CSharp):
    1. public class MoveShowerHead : MonoBehaviour
    2. {
    3.     [SerializeField] private float speedModifier;//2.5 normal speed
    4.     [SerializeField] private Camera minigameCamera;
    5.  
    6.     void OnMouseDrag()
    7.     {
    8.         //transform.GetChild(0).gameObject.GetComponent<ParticleSystem>().emission.enabled = true;
    9.     }
    10.     private void OnMouseUp()
    11.     {
    12.         //transform.GetChild(0).gameObject.GetComponent<ParticleSystem>().emission.enabled = false;
    13.     }
    14. }
    The line in both OnMouseDrag and OnMouseUp needs to be changed how should I do it ?
     
    Last edited: May 23, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Did you start with the documentation for the ParticleSystem emission module?

    I ask because the docs contain actual example code for enabling / disabling the emission module.

    If you're having trouble getting the reference example code to work, then...

    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!!!)

    Here's some more potentially-relevant reading:

    ParticleSystems (particlesystems): Do not use Play() / Stop() repeatedly: instead use Emit()

    https://forum.unity.com/threads/can...i-want-and-where-i-want.1146047/#post-7358438
     
  3. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    Yes I have done it the way it was written there but it gave me a bunch of errors.

    Here is the part of the code that is relevant
    Code (CSharp):
    1. private void OnMouseDrag()
    2.     {
    3.         ParticleSystem ps = transform.GetComponent<ParticleSystem>();
    4.         var em = ps.emission;
    5.         em.enabled = true;
    6.         //transform.GetChild(0).gameObject.GetComponent<ParticleSystem>().emission.enabled = true;
    7.     }
    8.     private void OnMouseUp()
    9.     {
    10.         ParticleSystem ps = transform.GetComponent<ParticleSystem>();
    11.         var em = ps.emission;
    12.         em.enabled = false;
    13.         //transform.GetChild(0).gameObject.GetComponent<ParticleSystem>().emission.enabled = false;
    14.     }
    And this is the error that this gives me

    NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
    UnityEngine.ParticleSystem+EmissionModule.set_enabled (System.Boolean value) (at <a0445b11e5c44656966b7efa420d7063>:0)
    MoveShowerHead.OnMouseUp () (at Assets/Scripts/MoveShowerHead.cs:22)
    UnityEngine.SendMouseEvents: DoSendMouseEvents(Int32)

    Will look into it thanks
     
    Last edited: May 23, 2023
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Errors have fixes and you don't need to post here to find them.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    Again, this has a solution and it ONLY has these steps:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  5. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I still don't understand the problem fully but here is the way I have solved it
    Code (CSharp):
    1. [SerializeField] private ParticleSystem showerParticle;
    2.  
    3.     private void OnMouseDrag()
    4.     {
    5.         var showerEmission = showerParticle.emission;
    6.         showerEmission.enabled = true;
    7.     }
    8.     private void OnMouseUp()
    9.     {
    10.         var showerEmission = showerParticle.emission;
    11.         showerEmission.enabled = false;
    12.     }
    Instead of getting the particlesystem from code like it was written in the documentation module. I made it so it gets it from inspector and it worked.

    Can't tell you why though.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Here's a clue: the example code is to demonstrate one narrow focus: how to manipulate the emission property.

    This focus is advanced. Very advanced. As such they expect that when you see a line like this (from the example code):

    Code (csharp):
    1.  ParticleSystem ps = GetComponent<ParticleSystem>();
    ... that you instantly understand how GetComponent<T>() works when you call it from a MonoBehaviour.

    You are saying, "Get the ParticleSystem Component that is on this same exact GameObject where this script is located."

    Perhaps that can help you see why it didn't find your PS.
     
  7. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I constantly use transfrom.GetComponent but this time it was diffirent for some reason. And in the error :
    "Do not create your own module instances, get them from a ParticleSystem instance"
    I thought that the reason was not that it was not able to find the component but the code creating its own particle system for some reason.
    Thinking that I tought that if GetComponent creates a new particle system for some reason I can just get it from the inspector and it worked.