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 Spergo · Jun 21, 2019 at 02:00 AM · particle system

Toggle particle system on or off

I'm trying to switch the particle system on and off with code. The programme starts with no rain / particles then is switched on or off by the player. The way I thought of doing it would be to turn emissions off so that the particles just peter out. The scripting reference link text

won't work. It seems because I'm declaring the particle system in Start() and switching it on or off in Update()

 void Start()
     {
         rainOff = GameObject.Find("sunCloud").GetComponent<SpriteRenderer>();
         toggleRain = GameObject.Find("rainCloud").GetComponent<SpriteRenderer>();
         toggleRain.enabled = false;
         ParticleSystem ps = GetComponent<ParticleSystem>();
         var em = ps.emission;
         em.enabled = false;
     }
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Vector3 pos = Input.mousePosition;
             Collider2D hitcollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));
             if (hitcollider != null && (hitcollider.CompareTag("cloud")))
             {
                 
 
                 if (!toggleRain.enabled)
                 {
                     toggleRain.enabled = true;
                     rainOff.enabled = false;
                     GetComponent<AudioSource>().Play();
                     //-some code here relating to the emissions- = true;
                     
                 }
                 else
                 {
                     toggleRain.enabled = false;
                     rainOff.enabled = true;
                     GetComponent<AudioSource>().Pause();
                     // -some code here relating to the emissions- = fa;se;
                 }
             }
 
         }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by $$anonymous$$ · Jun 21, 2019 at 02:37 AM

I think first you should declare your ParticleSystem (ps) variable outside the Start() method so you would have access to it from other parts of the code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DashboardUI : MonoBehaviour {
     public ParticleSystem ps;
 
     void Start() {
         rainOff = GameObject.Find("sunCloud").GetComponent<SpriteRenderer>();
         toggleRain = GameObject.Find("rainCloud").GetComponent<SpriteRenderer>();
         toggleRain.enabled = false;
         // Store the ParticleSystem component in the ps variable
         ps = GetComponent<ParticleSystem>();
     }
 
     void Update() {
         if (Input.GetMouseButtonDown(0)) {
             Vector3 pos = Input.mousePosition;
             Collider2D hitcollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));
             if (hitcollider != null && (hitcollider.CompareTag("cloud"))) {
 
                 if (!toggleRain.enabled) {
                     toggleRain.enabled = true;
                     rainOff.enabled = false;
                     GetComponent<AudioSource>().Play();
 
                     // Start the emission
                     ps.Play();
 
                 } else {
                     toggleRain.enabled = false;
                     rainOff.enabled = true;
                     GetComponent<AudioSource>().Pause();
 
                     // Stop the emission
                     ps.Play();
                 }
             }
 
         }
     }
 }


You can use

 ps.Play();


To start emitting your particles, and

 ps.Pause();


To stop emitting them.

Description of the ParticleSystem.Play() method

Starts the Particle System.

Sets the Particle Systems into play mode and enables emitting (if it has been disabled).

If the Particle System has been paused, then this resumes playing from the previous time. If the Particle System has stopped, then the system starts from time 0, and, if it is relevant, the startDelay is applied.


Link to the reference: https://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Jun 27, 2019 at 06:55 PM 0
Share

Did it work for you?

avatar image
0

Answer by Freesmith · Jun 21, 2019 at 02:11 AM

hi, i think you are missing this line

em.enabled = true; // where you want to start emission

or em.enabled = false;

where you want to stop it

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

113 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

Related Questions

Particle system radial velocity 1 Answer

Add collision planes to Shuriken ParticleSystem prefabs 1 Answer

Does the Shuriken Particle System in Unity 4 not allow particles to render prior to collision? 1 Answer

Particle System Values Don't affect 0 Answers

Particles in Orthographic Camera Disappearing Too Soon 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