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 /
  • Help Room /
avatar image
1
Question by Rok_Slana · Feb 25, 2018 at 06:29 PM · destroyparticlesystemmethod

How to destroy ParticleSystem clone?

Hi all. I am working on a simple game where the player shoots as a tank at targets. There I have explosions happening when projectile hits other objects - targets or ground. Since I want my explosions to be visible(and epic), Iam instantiating an ParticleSystem every time I need an explosion to occur. I have two scripts involved in explosions. They are:

 using UnityEngine;
 using System.Collections;
 
 public class ProjectileExplosion : MonoBehaviour {
 
 public float explosionForce = 5.0f;
 public float explosionRadius = 5.0f;

 private ExplosionHandler explosionHandler;
 private Vector3 explosionCoords;

 void OnCollisionEnter(Collision collision){

     explosionHandler = GameObject.FindObjectOfType<ExplosionHandler>();

     explosionCoords = collision.contacts[0].point;
     Collider[] colliders = Physics.OverlapSphere(explosionCoords, explosionRadius);
     explosionCoords.y+=1.5f;

     explosionHandler.explode(explosionCoords);

     foreach(Collider hitTarget in colliders){

         Rigidbody rigidBody = hitTarget.GetComponent<Rigidbody>();

         if(rigidBody != null){
             rigidBody.AddExplosionForce(explosionForce, explosionCoords, explosionRadius, 15.0f);
         }
     }
     Destroy(gameObject);
 }
 }

...and:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class ExplosionHandler : MonoBehaviour {

 public ParticleSystem ps1;
 public ParticleSystem ps2;

 private float delay = 4.0f;

 public void explode(Vector3 coords){
     ParticleSystem exp1 = Instantiate(ps1, coords, ps1.transform.rotation);
     ParticleSystem exp2 = Instantiate(ps2, coords, ps2.transform.rotation); 

     exp1.Play();
     exp2.Play();
     Destroy(exp1, delay);
     Destroy(exp2, delay);
 }
 }

First script simply detects the position of the collision and sends the coordinates to the other gameobject, which recieves these coordinates, instantiates two different explosions there, plays them and then it should also destroy these ParticleSystems. First script also releases the force of the explosion and destroys the projectile(successfully).

My problem is that Iam simply unable to destroy the particle system clones after they do their part and they keep on piling up in the hierarchy window. I've spent hours trying to handle this but unsuccessfully, did and re-did everything, tried with tags, layers, anything I could come up with or found on Google. I just can't get rid of them ParticleSystems. Can somebody pretty please explain to me what is the problem here that I seem to be missing? Any help, hint or example will be greatly appreciated! Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Rok_Slana · Feb 25, 2018 at 06:51 PM

Excuse me for spammin, I have just figured it out. I had to instantiate particle systems as GameObjects like so:

 public class ExplosionHandler : MonoBehaviour {

     public GameObject ps1;
     public GameObject ps2;

     private float delay = 4.0f;

     public void explode(Vector3 coords){
         GameObject exp1 = Instantiate(ps1, coords, ps1.transform.rotation) as GameObject;
         GameObject exp2 = Instantiate(ps2, coords, ps2.transform.rotation) as GameObject; 

         exp1.GetComponent<ParticleSystem>().Play();
         exp2.GetComponent<ParticleSystem>().Play();

         Destroy(exp1, delay);
         Destroy(exp2, delay);
     }
     }
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 UrielKane · Feb 25, 2018 at 06:56 PM

There are several ways of doing this. The most common solution and for this task is enough is to find or code a simple script that destroy himself after a certain amount of time has passed. var timeOut = 1.0; var detachChildren = false; function Awake () { Invoke ("DestroyNow", timeOut); } function DestroyNow () { if (detachChildren) { transform.DetachChildren (); } DestroyObject (gameObject); }

This is as script that i found years ago and i always use it when i just need something to destroy itself after instantiation. If you want this to destroy objects already on scene in loading and destroy when they become active you have to change the awake function for the start funcion. The one that i did myself even more years ago i used to use a yield waitforseconds statement with a float variable for timer. The detachChildren variables is for allow to destroy only the parent object owner of this script and not his children.

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 ugurgulser · Jan 18, 2020 at 12:01 AM

There is a "Stop Action" part in the Particle System. You can choose "Destroy". It works for me!

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

135 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 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

particleSystem has been destroyed but you are styll trying to access it 0 Answers

Particle Collision between particles 2 Answers

Unity 5: turning particles on/off 1 Answer

Disabling the bouncing of particles when colliding + ignoring trigger colliders 1 Answer

Play/Stop/Pause a Particule syste? 0 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