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
4
Question by Malaclypse · Sep 18, 2011 at 09:20 AM · particlesdestroy

Particles disappear when game object is destroyed...

I apoogize in advance if this has been asked. I couldn't find it.

I have a game object that emits particles as it flies. It is basically a magic bolt type effect. The game object is destroyed when it hits an enemy, but unfortunately all of the remaining living particles are also destroyed simultaneously instead of flickering out as they should.

Is there an easy way to prevent this? Please keep in mind that I am a noob to Unity, and a poor coder in general.

Thanks in advance for any help.

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

7 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by syclamoth · Sep 18, 2011 at 09:56 AM

If you want to have the particle effect fade out nicely, put the particle effect on a child object of your magic missile and then have something to the effect of the following code on the missile itself:

 public ParticleEmitter emit;

 // blah blah rest of code

 // Call this immediately before you destroy your missile
 public void DetachParticles()
 {
     // This splits the particle off so it doesn't get deleted with the parent
     emit.transform.parent = null;
 
     // this stops the particle from creating more bits
     emit.emission = 0;
 
     // This finds the particleAnimator associated with the emitter and then
     // sets it to automatically delete itself when it runs out of particles
     emit.GetComponent<ParticleAnimator>().autoDestruct = true;
 }

And then you're all set! Assign the particleEmitter object to your script in the inspector, and then when you want to delete your missile, call that bit of code and it will neatly separate the particle effect at the same time as ordering it to clean itself up when it's finished.

Comment
Add comment · Show 3 · 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 Malaclypse · Sep 19, 2011 at 05:10 AM 0
Share

I had a little trouble because I think your code is in C#, and my script is in js, but I worked at it, and your answer helped me immensely. With great people here willing to help, I think I may actually be able to learn this given enough time.

Thank you so much :)

avatar image syclamoth · Sep 19, 2011 at 08:24 AM 2
Share

Sorry about the C# thing- I keep forgetting that most people here use JS! Can't stand it personally...

avatar image Malaclypse · Sep 19, 2011 at 08:38 AM 0
Share

No problem at all. I have no strong preference for it except that the script reference (which often leaves me more confused than before I looked at it), and many of the tutorials are in js. As far as I can tell no language is particularly better than the other for this, and I often find myself wishing there was only one language being used.

I'm sure the coding gurus can jump back and forth with ease, but learning one new language is hard enough. Thanks again for the help.

avatar image
3

Answer by djirving82 · Oct 29, 2018 at 10:22 AM

I used a little from the other answers here and this code works great for me. This code is for multiplayer setups, remove the "[Command]" and "Cmd" if you are not using NetworkBehaviour for multiplayer. Also make sure to change your Stop Action to "destroy" in your Particle System.

  public ParticleSystem emit;
  // blah blah rest of code
 
  // Call this immediately before you destroy your missile
     [Command]
     void CmdDetachParticles()
     {
         // This splits the particle off so it doesn't get deleted with the parent
         emit.transform.parent = null;
 
         // this stops the particle from creating more bits
         emit.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
avatar image
1

Answer by jsnetors · Sep 29, 2015 at 09:07 AM

For anyone having this problem in Unity5, this solution won't work as-is.

Unity 5 took away many legacy things, ParticleAnimator included.

The solution I found and what should work to most people using U5 is as follows:

This is a code that puts AutoDestroy back to the particle. You just create a new script, put this code inside and then put the code inside the particleSystem you want to terminate:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class    AutoDestroy : MonoBehaviour
 
 //This code destroys the particle's GameObject once it's Start Time is over.
 //Also, if you're having issues with particles vanishing after you get far from them, just set their XYZ Scale values to 2, 2, 2
 
 {
     private float timeLeft;
 
 public void Awake()
     {
         ParticleSystem system = GetComponent<ParticleSystem>();
         timeLeft = system.startLifetime;
     }
     public void Update()
     {
         timeLeft -= Time.deltaTime;
         if (timeLeft <= 0)
         {
             GameObject.Destroy(gameObject);
         }
     }
 }


Now, to the projectile gameObjects script:

 public ParticleSystem emit;
 
 void OnTriggerEnter2D(Collider2D other) //Can be other ways of detecting
     
 {
         // This splits the particle off so it doesn't get deleted with the parent
         emit.transform.parent = null;
 
         // this stops the particle from creating more bits
         emit.emissionRate = 0;
 
         //This destroys the projectile
         Destroy (gameObject);
   
   }

Peace.

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
avatar image
0

Answer by qiaosic · Nov 16, 2016 at 08:38 PM

Thanks for all the answers above, here is the one that work for me. Other solution for Unity 5 without using autoDestruct . Using ParticleSystem.IsAlive().

         private void Update()
         {
             // check if the particle system still alive.
             if (_detached 
                 && null != _curParticleSystem 
                 && !_curParticleSystem.IsAlive())
             {
                 if (null != _parent)
                 {
                     //put it back to the parent.
                     transform.parent = _parent;
                     _detached = false;
                 }
                 else
                 {
                     // destroy it.
                     Destroy(this);
                 }
             }
         }
 
         public void Detach()
         {
             _detached = true;
             _parent = transform.parent;
 
             // splits the particle off so it doesn't get deleted with the parent
             transform.parent = null;
 
             _curParticleSystem = GetComponent<ParticleSystem>();
             if (null == _curParticleSystem)
             {
                 return;
             }
 
             // stops the particle from creating more bits
             _curParticleSystem.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
avatar image
0

Answer by killakiwi · Aug 04, 2017 at 07:31 AM

I wrote this after I couldn't find anything that I was happy with:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class ParticleDestroyer : MonoBehaviour {
 
     static List<GameObject> ParticleSystemGameObjects = new List<GameObject>();
     
     void Update () {
         CheckAndDestroyDeadParticleSystems ();
     }
 
     static void CheckAndDestroyDeadParticleSystems(){
         for (int i = 0; i < ParticleSystemGameObjects.Count; i++) {
             bool AParticleSystemStillAlive = false;
 
             ParticleSystem[] ParticleSystems = ParticleSystemGameObjects [i].GetComponents<ParticleSystem> ();
             for (int j = 0; j < ParticleSystems.Length; j++){
                 if (ParticleSystems[j].IsAlive ())
                     AParticleSystemStillAlive = true;
             }
 
             if (!AParticleSystemStillAlive) {
                 GameObject.Destroy (ParticleSystemGameObjects [i]);
                 ParticleSystemGameObjects.Remove (ParticleSystemGameObjects [i]);    
             }
         }
     }
 
     public static void Destroy(GameObject ParticleSystemToDestroy){
         ParticleSystemGameObjects.Add (ParticleSystemToDestroy);
 
         ParticleSystemToDestroy.transform.parent = null;
 
         foreach(ParticleSystem ParticleSystem in ParticleSystemToDestroy.GetComponents<ParticleSystem>()){
             ParticleSystem.emissionRate = 0;
             ParticleSystem.Stop ();
         }
     }
 }

You call it like this:

 public void SwordSlash_Finished () {
     ParticleDestroyer.Destroy(Particle);
     Destroy(gameObject);
 }

So the ParticleDestroyer is a class which uses static methods, the class itself is not static because I want to use the Update function, which can only be used off of classes that are derived from MonoBehaviour. This means you need to attack the script to a GameObject in your scene.

The Destroy function halts the particle systems activities and the CheckAndDestroyDeadPArticleSystems function constantly scans all particle systems and if there are no dead alive particles left it can safely destroy the GameObject. It also accounts for GameObjects with multiple ParticleSystems attached to them.

Your welcome.

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
  • ›

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

9 People are following this question.

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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Particle System lifetime not affecting particles 0 Answers

How to destroy / hide a single particle? 2 Answers

Trouble with destroying a particle. 1 Answer

Trying to make a dust particle when the the player moves 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