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

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 :wink:

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;
		}
	}