Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cole-Ossus · Jan 18, 2021 at 10:56 AM · c#particlesparticlesystem

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cole-Ossus · Jan 18, 2021 at 12:41 PM

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you change the Velocity Over Lifetime of a Particle System using a Script? 3 Answers

Assign random start lifetime for particle system from script 2 Answers

Turning my grid of particles into a sphere 0 Answers

Can someone tell me how to make a particle system play when hitting an animal? 0 Answers

How to make an effect in a particle system so that it is always behind the sphere? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges