Particle System Play/Stop with SpaceKey

Hi guys, I’m trying to get a particle system to work with a spray can GameObject I’ve found on 3d warehouse. I am familiar with C# and JavaScript but have absolutely no experience with game development or Unity.

I have a GameObject tagged with SprayCan which has a child object, SprayParticleSystem.

My script is pretty simple, and all I’d like to do is have the spray can emit particles when the space bar is pressed.

    GameObject sprayCan;

	// Use this for initialization
	void Start () {
		//sprayCan = GameObject.FindWithTag("SprayCan");
		sprayCan = GameObject.FindWithTag("SprayParticleSystem");
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetKeyDown(KeyCode.Space))
		{
			sprayCan.particleSystem.enableEmission = true;
			sprayCan.particleSystem.Play();
		}
		else
		{
			sprayCan.particleSystem.enableEmission = false;
			sprayCan.particleSystem.Stop();
		}	
	}

I have the script attached to the SprayCan object (just by dragging and dropping the script on the object). What am I doing wrong?

Try This Hope You Are looking For This…

GameObject sprayCan;

// Use this for initialization
void Start () {
	//sprayCan = GameObject.FindWithTag("SprayCan");
	sprayCan = GameObject.FindWithTag("SprayParticleSystem");
}

// Update is called once per frame
void Update () {
	
	if (Input.GetKeyDown (KeyCode.Space)) 
	{
		EnableDisableParticle(true);
	}
	if (Input.GetKeyUp (KeyCode.Space)) 
	{
		EnableDisableParticle(false);
	}
}
void EnableDisableParticle(bool flag)
{
	sprayCan.particleSystem.enableEmission = flag;
	if(flag)
		sprayCan.particleSystem.Play();
	else
		sprayCan.particleSystem.Stop();
}