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