Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 laszlar · May 04, 2016 at 03:49 AM · particlesystemshurikenemissionenabled

How to use emission.enable correctly?

Hi everyone!

I'm trying to get a particle system to be enabled only when pressing down the space or left mouse click. This code below works, but I feel that it's quite inefficient (using getcomponent in update).

 using UnityEngine;
 using System.Collections;
 
 public class FartEffect : MonoBehaviour
 {
     public ParticleSystem fart;
 
     void Start()
     {
        /* ParticleSystem fart = GetComponent<ParticleSystem>();
         ParticleSystem.EmissionModule isFarting = fart.emission;
         isFarting.enabled = false;
         //farting = GetComponent<ParticleSystem>().emission.enabled = false;
         //farting.enabled = false;*/
     }
     
 
     void Update ()
     {
         if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
         {
             ParticleSystem fart = GetComponent<ParticleSystem>();
             ParticleSystem.EmissionModule isFarting = fart.emission;
             isFarting.enabled = true;
         }
         else
         {
             ParticleSystem fart = GetComponent<ParticleSystem>();
             ParticleSystem.EmissionModule isFarting = fart.emission;
             isFarting.enabled = false;
         }
     }
 }

Also, Is there an easy way to make the particle system play just a quarter second longer, instead of the brief split second burst (according to the code)?

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
1

Answer by Addyarb · May 04, 2016 at 04:19 AM

Here is a simplified version of your above script. But before you read this, please consider the following for future usage:

If you have any questions, please leave a comment below this answer, and I'd be happy to help you out. Otherwise, please mark the answer as correct by clicking the check mark below the thumbs-down button!

If you want to define a pre-defined variable (i.e. one that is defined outside of a function), do not declare it's type before you define it. For instance:

  public GameObject go;
     void Start(){
     go = GameObject.Find("GameObject");
     }

Instead of

 public GameObject go;
 void Start(){
 GameObject go = GameObject.Find("GameObject");
 }

If you define the type before the variable name, the compiler assumes you are wanting to name it as a temporary variable, which will only exist within that function. If you try to reference it anywhere else - you will get an error.

I hope this rewrite will help you, and perhaps sway you from your toilet humor ;)

 using UnityEngine;
 
 public class FartEffect : MonoBehaviour
     {
     public ParticleSystem.EmissionModule airFreshener;
     public float timer;
 
     void Update()
         {
         SprayAirFreshener();
         CheckInput();
         SubtractTime();
         }
 
     void CheckInput()
         {
         //If we click the mouse or strike the space bar, add 1/4 of a second to the timer
         if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) timer += 0.25f;
         }
 
     void SprayAirFreshener()
         {
         //If the timer is greater than zero, enable the air freshener
         airFreshener.enabled = (timer > 0) ? true : false;
         }
 
     void SubtractTime()
         {
         //Subtract 1 second from the timer each second, and set it to zero if less than 1 second
         timer -= Time.deltaTime;
         timer = (timer <= 0) ? 0 : timer;
         }
     }
Comment
Add comment · Show 2 · 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
avatar image laszlar · May 04, 2016 at 02:32 PM 0
Share

@Addyarb I think this just might work! Let me run and see. Thank you for your help, seriously. I see there are quite a few answers out there regarding this (99% obsolete, of course), but they were all kind of different in their use. One additional question I have, if you don't $$anonymous$$d - I have this script directly on the particle effect (technically it is the grandchild to the player/player movement script). Would you see any reason that this would butt heads with the player movement script that already has a if(Input.Get$$anonymous$$eyDown($$anonymous$$eycode.Space)) AddForce.. AND/OR should I just throw this part of the script in using GetComponentInChildren and essentially just combine the two scripts? BTW that playermovement script is a structured mess lol.

avatar image laszlar · May 04, 2016 at 02:44 PM 0
Share

@Addyarb AH, see this is where it falls back to the original problem that I was having. The particle system keeps playing on awake - which in the editor you can uncheck (Play on Awake*) - however, in doing so it prevents it from playing altogether. Am I missing something here?

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

42 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

Related Questions

Shuriken "Distance" emission mode not working 1 Answer

Particle System Collision: Collisions and Trigger 0 Answers

square shaped particle emitter? 1 Answer

Manually Placing Particles 2 Answers

ParticleSystem Rate over Distance doesn't work until poked 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