How to Activate/Deactivate Particle System with one button

Hi Everyone,

I have a particle system called “Fire” and I want to activate and deactivate it using the same key. ie: When It’s off and I click “Q” it should activate, but when it’s on and I click “Q” it should deactivate. With the code I have below, when I assign “Q” for both actions it activates but doesn’t deactivate upon second input, however, when I assign “Q” for one action and “E” for the other action, it works. I also have “Play on Awake” unticked. What am I missing?

Please note:
You will notice I reference another particle system “Ice” which isn’t being utilised. I do intend to have this script reference many different particle systems in the future so if you have any further advice you’d like to share, please do :).

I have tried several ways but nothing works. Please help. There’s probably a more efficient way of doing this, but I’m just looking for functionality right now:

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Elements : MonoBehaviour
{
public ParticleSystem Fire;
public ParticleSystem Ice;

//Referencing child objects (particles) in parent
void Awake()
{
    Fire = GameObject.Find("Fire").GetComponent<ParticleSystem>();
    Ice = GameObject.Find("Ice").GetComponent<ParticleSystem>();
}

// Update is called once per frame
void Update()
{
    //Elements
    //Activate and deactivate child object "Fire" by pressing assigned key
    if (Input.GetKey("q") & Fire.IsAlive() == false)
    {
        Fire.Play();
    }
    
    if (Input.GetKey("e") & Fire.IsAlive() == true) //I want this to also be "Q"
    {
        Fire.Stop();
    }

    //Activate and deactivate child object "Ice" by pressing assigned key
    if (Input.GetKey("r") & Ice.IsAlive() == false)
    {
        Ice.Play();
    }

    if (Input.GetKey("f") & Ice.IsAlive() == true) //I want this to also be "R"
    {
        Ice.Stop();
    }
}

}

Hi everyone,

(Posted this in comments earlier instead of answer… my bad lol)
So it looks like all I needed was an “else if” after the initial “if” statement. It does seem a little bit buggy every now and then (I assume this has something to do with the Particle Lifetime perhaps because it’ll start normally but when I stop it I can’t immediately start it again, I think because I have to wait for all previous particles to disappear), for now, I’m satisfied with the outcome. If I do make any improvements, I’ll be sure to post. If you’d like, feel free to use my code. I’ve also opted to utilise the number keys for now as this makes more sense for my intended purpose (any key will work though.)

Any advice on how I could possibly do this better?

Revised Code

using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Elements : MonoBehaviour
{
public ParticleSystem Ice; public ParticleSystem Fire;
//Referencing child objects (particles) in parent
void Awake()
{
Fire = GameObject.Find(“Fire”).GetComponent();
Ice = GameObject.Find(“Ice”).GetComponent();
}

// Update is called once per frame
void Update()
{
    //Elements
    /*
        4 = Ice
        6 = Fire
     */
    //Activate and deactivate child object "Fire" by pressing assigned key
    if (Input.GetKey("6") & Fire.IsAlive() == false)
    {
        Fire.Play();
    }

    else if (Input.GetKey("6") & Fire.IsAlive() == true) //I want this to also be "6"
    {
        Fire.Stop();
    }
    //Activate and deactivate child object "Ice" by pressing assigned key
    if (Input.GetKey("4") & Ice.IsAlive() == false)
    {
        Ice.Play();
    }
    else if (Input.GetKey("4") & Ice.IsAlive() == true) //I want this to also be "4"
    {
        Ice.Stop();
    }
}

}