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 /
This question was closed Feb 08, 2015 at 11:14 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
19
Question by MickeyBlumental · Feb 21, 2012 at 10:20 PM · particlesparticle system

Auto-destroying particle system

I'm going through the 3D Buzz 2D shooter tutorial series and they obviously use an earlier version of Unity as the options for particle systems has changed beyond recognition. In the older version there was a tick box to tell the particle system to auto-destroy once it's finished playing. The new version of Unity doesn't have that option and as a result every time I kill an enemy an empty particle system is left behind after the explosion finished playing, which is obviously not good.

How do I destroy them now?..

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

11 Replies

  • Sort: 
avatar image
50
Best Answer

Answer by Jenster · Jun 12, 2012 at 05:36 AM

I have some particle systems that are one-shots, so here is what I do to auto-destroy them when they are complete. IsAlive() is the key method. Just attach this script, fire off the system any way you like, and it will autodestroy.

 private var ps : ParticleSystem;
 
 ////////////////////////////////////////////////////////////////
 
 function Start () {
          ps = GetComponent(ParticleSystem);
 }
 
 function Update () {
     if(ps)
     {
         if(!ps.IsAlive())
         {
             Destroy(gameObject);
         }
     }
 }
Comment
Add comment · Show 11 · 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 RKSandswept · Jun 16, 2014 at 07:32 PM 5
Share

In C#

 using UnityEngine;
 using System.Collections;
 
 public class ParticleSystemAutoDestroy : $$anonymous$$onoBehaviour 
 {
     private ParticleSystem ps;
 
 
     public void Start() 
     {
         ps = GetComponent<ParticleSystem>();
     }
 
     public void Update() 
     {
         if(ps)
         {
             if(!ps.IsAlive())
             {
                 Destroy(gameObject);
             }
         }
     }
 }
avatar image jtsmith1287 · Oct 21, 2014 at 10:45 PM 0
Share

This is the winner right here. This needs to be accepted as the answer. It makes the most sense, if you're wanting the particles to destroy themselves on completion, do just have them destroy themselves on completion, lol. No need to specify the time. :)

avatar image LilGames jtsmith1287 · Mar 05, 2021 at 07:31 PM 0
Share

Some truth here, but ideally you'd want to return the particle system to a pool for re-use later. Constantly creating/instantiating/destroying objects can/will impact performance.

avatar image theonlybrien · Feb 08, 2015 at 08:48 AM 0
Share

Well done. I implemented this on my project and it worked flawlessly. Thanks!

avatar image buzzefall · Mar 24, 2015 at 07:33 PM 6
Share

I think this will be slightly easier. Came up with this just after watching one of a good standard tutorials (Space Shooter):

Attach this script right to the Particle System object:

 using UnityEngine;
 using System.Collections;
 
 public class DestroyParticles : $$anonymous$$onoBehaviour
 {
 
     private IEnumerator Start()
     {
         yield return new WaitForSeconds(GetComponent<ParticleSystem>().duration);
         Destroy(gameObject); 
     }
     
 }

Literally we are interrupting Start() method to wait for ParticleSystem.duration in seconds, then we get back in method and continuing from next line and destroying ParticleSystem object. Not very complex code, and i think it more efficient, because you don't need to call method update for checking =)

Because I don't know deep theory well, i must only say: for more info take a look at Space Shooter tutorial (video number 12-13) and Coroutine tutorials linked below on of them.

That guy below got even better solution... Destroy with "duration" delay:

 using UnityEngine;
 using System.Collections;
 
 public class DestroyParticles : $$anonymous$$onoBehaviour
 {
 
     private void Start()
     {
         Destroy(gameObject, GetComponent<ParticleSystem>().duration); 
     }
     
 }
avatar image KevinCodes4Food · Mar 25, 2015 at 11:30 PM 0
Share

The issue with using a time driven Coroutine or Destroy is time scale changes and pauses. If you turn on slow motion after the system starts, the particle system in will then end before it should. $$anonymous$$y answer below has the same issue using Destroy, which is fine if you do not have slow motion or do not care that the particle system will end during pause. If time scaling does matter in your project, an Update based solution will be better, such as the one from RkSandswept.

Also, using only 'duration' will not give time for the last emitted particles to burn out. See my solution below with 'StartLifetime' added to allow particles emitted at the end of the duration to linger on appropriately.

avatar image OneManEscapePlan KevinCodes4Food · Mar 17, 2016 at 02:07 AM 0
Share

@$$anonymous$$evinCodes4Food Coroutines that use WaitForSeconds are affected by Time.timeScale; so is Invoke. Go ahead and try it out. The only one I'm not sure about is Destroy.

Show more comments
avatar image
6

Answer by fireomega · Feb 22, 2012 at 08:23 PM

A simple Destroy code and then just add the time the particle lasts for. e.g. say the time it lasts for is 5 seconds than you do: Destroy(gameObject, 5);

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 fireomega · Feb 22, 2012 at 09:17 PM 2
Share

If this answered your question please click on the tick

avatar image masterchafe · Mar 16, 2012 at 08:34 AM 0
Share

thanks fireomega, that was perfect for me as I'm still using u3.4. When my missile object (that has the particle system attached to it) gets a signal to explode, this is how I handle the smoke trail:

 particleSystem.particleEmitter.emit=false; 
 particleSystem.transform.parent=null; // detach particle system
 Destroy(particleSystem.gameObject, 3);
 Destroy(gameObject);
avatar image
3

Answer by RogueHydra · Mar 08, 2012 at 12:14 AM

I saw this and made a minor adjustment. It works for me in my situation but I'm fairly new to using Unity so I'm not sure how valid it will be for anyone else. This lets you put it on any particle system and it will kill it when it's done playing (no need to set the duration).

 var psys = this.GetComponent(ParticleSystem);
 Destroy(this.gameObject, psys.duration);
Comment
Add comment · Show 4 · 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 _MGB_ · Sep 06, 2012 at 09:40 PM 1
Share

This is the simplest solution, without the need to attach scripts. I'd make one small change to the duration though to ensure no particles are cut off in their prime:

Object.Destroy(gameObject, psys.duration + psys.startLifetime);

avatar image windterfresh · Dec 06, 2013 at 08:49 PM 0
Share

This is even simpler. Only 1 line of code.

Destroy(gameObject, gameObject.particleSystem.duration);

avatar image KevinCodes4Food · Jan 18, 2014 at 08:40 PM 1
Share

The solution from $$anonymous$$GB and windterfresh works perfectly, and makes for a very simple and reliable auto destruct component that can be added to any particle system object.

Here is the complete script:

 using UnityEngine;
 using System.Collections;
 
 public class ParticleSystemAutoDestroy : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
         if(gameObject.particleSystem)
         {
             GameObject.Destroy(gameObject, gameObject.particleSystem.duration + gameObject.particleSystem.startLifetime);
         }
     }
 }

   
avatar image Regretfulone · Feb 25, 2014 at 07:35 PM 0
Share

Thanks $$anonymous$$evinCodes4Food! The solution you added above worked perfectly for what I needed! I am no longer getting dead particle systems hanging around wasting resources :]

avatar image
2

Answer by Imi · Mar 07, 2012 at 11:46 PM

I recommend the following script, as it does not need to know how long the particle system will go (this can vary depending on the start lifetime and in worst case a lot of curves..)

 function Update ()
 {
   if (particleSystem != null && particleSystem.particleCount == 0)
     Destroy(gameObject);
 }
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 Dakwamine · Mar 18, 2012 at 11:56 PM 0
Share

I would recommend to use LateUpdate() ins$$anonymous$$d of Update(). Thanks for your code anyway. ;)

avatar image
1

Answer by RogueHydra · Mar 08, 2012 at 12:14 AM

I modified it just a little so you can just attach this to any particle system and it'll kill it after the duration is up. I'm a not sure if this'll work for everyone but it works perfect for me and all my systems (haven't had any issues and haven't seen any bugs yet).

function Start () {

var psys = this.GetComponent(ParticleSystem);

Destroy(this.gameObject, psys.duration);

}

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
  • 1
  • 2
  • 3
  • ›

Follow this Question

Answers Answers and Comments

25 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

Related Questions

How to pause emitter in new Particle System? 2 Answers

How do you change the colour of individual particles having assigned them a material? 1 Answer

particles dont have color 2 Answers

Particle System - Texture Sheet Animation, Flip U/V greyed out? 1 Answer

How to keep particles moving past an obstacle after they have collided with it? 3 Answers


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