4

i am trying to turn on and off a particle system i created.
I attached it to a prefab.

The code I am using is as follows

public ParticleSystem waterGun;

void Update () {
    if(Input.GetKey(KeyCode.W)){
        waterGun.enableEmission = true;
    }else if(Input.GetKeyUp(KeyCode.W)){
        waterGun.enableEmission = false;
    }
}

I want the particle system to play in front of the fps when a key is held down and stop playing when it is pressed.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Alan Fletcher
  • 283
  • 1
  • 13
  • 24
  • According to the [Unity3D documentation](http://docs.unity3d.com/Documentation/ScriptReference/ParticleSystem-enableEmission.html) this _should_ disable the emission. I take it this is not working for you? – Chris Sinclair Mar 13 '13 at 20:50
  • unfortunately it is not, thats wats confusing me – Alan Fletcher Mar 13 '13 at 20:52
  • 1
    Have you properly assigned the correct instance of the particle system to the `waterGun` field? – Chris Sinclair Mar 13 '13 at 20:54
  • the waterGun prefab is given to it – Alan Fletcher Mar 13 '13 at 20:58
  • Is it possible the `if`statement should be changed to `GetKeyDown` instead of `GetKey`? In your question, you say: `and stop playing when it is pressed.` This is not what happens here. Here, it'll stop playing when you release the button. – Joetjah Mar 14 '13 at 08:03

2 Answers2

5

Try using:

waterGun.Play();

and

waterGun.Stop();

And also, your logic is inverted, like Joetjah said.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Corne
  • 496
  • 2
  • 8
  • 22
0

You say that the "waterGun prefab is given to it" but you should be assigning an instance of a particle system in the scene to waterGun, not the prefab. A prefab doesn't exist in the scene.

Sam
  • 3,320
  • 1
  • 17
  • 22