- Home /
 
How to use emission.enable correctly?
Hi everyone!
I'm trying to get a particle system to be enabled only when pressing down the space or left mouse click. This code below works, but I feel that it's quite inefficient (using getcomponent in update).
 using UnityEngine;
 using System.Collections;
 
 public class FartEffect : MonoBehaviour
 {
     public ParticleSystem fart;
 
     void Start()
     {
        /* ParticleSystem fart = GetComponent<ParticleSystem>();
         ParticleSystem.EmissionModule isFarting = fart.emission;
         isFarting.enabled = false;
         //farting = GetComponent<ParticleSystem>().emission.enabled = false;
         //farting.enabled = false;*/
     }
     
 
     void Update ()
     {
         if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
         {
             ParticleSystem fart = GetComponent<ParticleSystem>();
             ParticleSystem.EmissionModule isFarting = fart.emission;
             isFarting.enabled = true;
         }
         else
         {
             ParticleSystem fart = GetComponent<ParticleSystem>();
             ParticleSystem.EmissionModule isFarting = fart.emission;
             isFarting.enabled = false;
         }
     }
 }
 
               Also, Is there an easy way to make the particle system play just a quarter second longer, instead of the brief split second burst (according to the code)?
Answer by Addyarb · May 04, 2016 at 04:19 AM
Here is a simplified version of your above script. But before you read this, please consider the following for future usage:
If you have any questions, please leave a comment below this answer, and I'd be happy to help you out. Otherwise, please mark the answer as correct by clicking the check mark below the thumbs-down button!
If you want to define a pre-defined variable (i.e. one that is defined outside of a function), do not declare it's type before you define it. For instance:
  public GameObject go;
     void Start(){
     go = GameObject.Find("GameObject");
     }
 
               Instead of
 public GameObject go;
 void Start(){
 GameObject go = GameObject.Find("GameObject");
 }
 
               If you define the type before the variable name, the compiler assumes you are wanting to name it as a temporary variable, which will only exist within that function. If you try to reference it anywhere else - you will get an error.
I hope this rewrite will help you, and perhaps sway you from your toilet humor ;)
 using UnityEngine;
 
 public class FartEffect : MonoBehaviour
     {
     public ParticleSystem.EmissionModule airFreshener;
     public float timer;
 
     void Update()
         {
         SprayAirFreshener();
         CheckInput();
         SubtractTime();
         }
 
     void CheckInput()
         {
         //If we click the mouse or strike the space bar, add 1/4 of a second to the timer
         if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) timer += 0.25f;
         }
 
     void SprayAirFreshener()
         {
         //If the timer is greater than zero, enable the air freshener
         airFreshener.enabled = (timer > 0) ? true : false;
         }
 
     void SubtractTime()
         {
         //Subtract 1 second from the timer each second, and set it to zero if less than 1 second
         timer -= Time.deltaTime;
         timer = (timer <= 0) ? 0 : timer;
         }
     }
 
              @Addyarb I think this just might work! Let me run and see. Thank you for your help, seriously. I see there are quite a few answers out there regarding this (99% obsolete, of course), but they were all kind of different in their use. One additional question I have, if you don't $$anonymous$$d - I have this script directly on the particle effect (technically it is the grandchild to the player/player movement script). Would you see any reason that this would butt heads with the player movement script that already has a if(Input.Get$$anonymous$$eyDown($$anonymous$$eycode.Space)) AddForce.. AND/OR should I just throw this part of the script in using GetComponentInChildren and essentially just combine the two scripts? BTW that playermovement script is a structured mess lol.
@Addyarb AH, see this is where it falls back to the original problem that I was having. The particle system keeps playing on awake - which in the editor you can uncheck (Play on Awake*) - however, in doing so it prevents it from playing altogether. Am I missing something here?
Your answer