Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Oct 12, 2013 at 08:50 PM by Reefer for the following reason:

SOLVED

avatar image
0
Question by Reefer · Oct 11, 2013 at 08:42 PM · c#gameobjectparticlesdestroy

Particle system not destroying.

I got an problem with particle system. I'm instantiating one everytime ball hits an brick, and after it's done it should be removed automaticly, but these just won't get destroyed.

Script doesnt give out any errors or warnings, but particle system(clone)'s just won't get destroyed.

Here's the whole script:

 using UnityEngine;
 using System.Collections;
 
 public class BrickScript : MonoBehaviour {
     
     static int numBricks = 0;
     public int pointValue = 1;
     public int currentLvl = 0;
     public int hitPoints = 1;
     public int powerUpChance = 3;
     
     public Transform particlesPrefab;
     
     public GameObject[] powerUpPrefabs;
     
     public AudioClip[] hitaudio;
     
     // Use this for initialization
     void Start () {
         numBricks++;
     }
     
     // Update is called once per frame
     void Update () {
     if ( currentLvl == 4) {
             Application.LoadLevel("GameOver");
         }
 
     }
     
     void OnCollisionEnter ( Collision col ) {
         hitPoints--;
         if ( hitPoints <= 0 ) {
             Die();
         }
         if (col.gameObject.tag == "Ball")
             Debug.Log ("Particles!");
         GameObject go = Instantiate(particlesPrefab, transform.position, Quaternion.identity) as GameObject;
         Destroy (go, 2.5f);
     }
     
     void Die() {
     Destroy ( gameObject );
         BallScript ball1Script= GameObject.FindGameObjectWithTag ("Ball").GetComponent<BallScript>();
         PaddleScript paddleScript= GameObject.Find ("Paddle").GetComponent<PaddleScript>();
         paddleScript.AddPoint(pointValue * (int)ball1Script.curspeed);
         
         numBricks--;
         
         Debug.Log (numBricks);
         
         if ( Random.Range(0, powerUpChance) == 0 ) {
         Instantiate( powerUpPrefabs[ Random.Range(0, powerUpPrefabs.Length) ] , transform.position, Quaternion.identity );
          }
         
         if ( numBricks <= 0 ) {
             //Load Next Level
         Application.LoadLevel(currentLvl+1);
         if(currentLvl == 1){
         GJAPIHelper.Trophies.ShowTrophyUnlockNotification (4086);
         }
         if(currentLvl == 2){
         GJAPIHelper.Trophies.ShowTrophyUnlockNotification (4087);
         }
         }
     }
 }


And here's just the part that SHOULD be removing that.

 void OnCollisionEnter ( Collision col ) {
         hitPoints--;
         if ( hitPoints <= 0 ) {
             Die();
         }
         if (col.gameObject.tag == "Ball")
             Debug.Log ("Particles!");
         GameObject go = Instantiate(particlesPrefab, transform.position, Quaternion.identity) as GameObject;
         Destroy (go, 2.5f);
     }


Why it isnt getting destroyed?

Comment
Add comment · Show 2
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 InspGadget · Jan 19, 2016 at 05:47 PM -1
Share

A simple way to get a particle system to self destroy is to attach a script component to the Particle System object and run this code

void Start() { Destroy(gameObject, GetComponent().duration); }

This sets an Auto Destroy on the Particle System Object based on its set duration

avatar image thetimmy · Jan 05 at 07:35 PM 0
Share

I'm a complete noob and this might be already clear to everyone, but I was running into this same problem and found a very simple solution for me. When I edit the particlesystem, in the inspector there is a setting that controls what happens when the particles have stopped. It's called "Stop Action". Mine was set to "None" and the particlesystem objects would persist. But when I changed that setting to "Destroy", they were removed from the Hierarchy.

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by Tomer-Barkan · Oct 12, 2013 at 06:43 AM

I have the following script attached to the particle system prefab, and it works fine:

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

 public class HitEmitterScript : MonoBehaviour {
     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);
         }
     }
 }


Note that the lifetime is set to be the particle's starting life, but you could make it a constant 2.5 seconds.

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 Owen-Reynolds · Oct 12, 2013 at 04:16 PM 1
Share

The more Unity way of doing that is changing the countdown (timeLeft) into a clock time when you should die.

Say the variable is renamed destroyTime. Set in start destroyTime=Time.time+2.5f;. Then update checks if(Time.time>=destroyTime). No need for the decrement.

avatar image Tomer-Barkan · Oct 12, 2013 at 05:36 PM 0
Share

Good to know, thanks :)

avatar image Reefer · Oct 12, 2013 at 07:19 PM 0
Share

I will try that tonight later and let you know.

avatar image Reefer · Oct 12, 2013 at 08:49 PM 0
Share

This works with my game, thank you

avatar image
1

Answer by Doireth · Oct 11, 2013 at 08:47 PM

Particle systems don't self destroy. It's a problem that a lot of people have. A better option would be to not instantiate the particle system every time but rather instantiate it once at the start, move it to where it needs to be (such as a collision point) and use

 [Particle System].Play()

This way it removes needless instantiation and the need for it to be destroyed as you'll be using it over and over. Better performance too.

Comment
Add comment · Show 6 · 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 Reefer · Oct 11, 2013 at 09:12 PM 0
Share

Ok, but I'd still like to know why my code isnt deleting it?

avatar image Doireth · Oct 11, 2013 at 09:22 PM 0
Share

When Unity changed from their legacy particle system to the current one (known as Shuriken) this problem has emerged quite a bit. $$anonymous$$any people have requested that particle systems self-destruct but it hasn't been implemented yet. I really don't know why it doesn't destroy when you destroy what it's attached to and it's something that has cause me grief before. You could try (when destroying the object) to destroy the particle component itself. For example.

 Destroy(go.transform.GetComponent<ParticleSystem>());
avatar image Reefer · Oct 11, 2013 at 09:29 PM 0
Share

This gives only an error:

"NullReferenceException: Object reference not set to an instance of an object"

avatar image Doireth · Oct 11, 2013 at 09:30 PM 0
Share

Your code destroys the entire object. I was suggest destroying the particle system component itself.

avatar image Doireth · Oct 11, 2013 at 09:32 PM 0
Share

Ok, try

 ParticleSystem go = Instantiate(particlesPrefab, transform.position, Quaternion.identity) as ParticleSystem;
 
 Destroy(go, 2.5f);
Show more comments
avatar image
1

Answer by Owen-Reynolds · Oct 11, 2013 at 09:36 PM

The code (Instantiate then timed 2.5-sec Destroy) looks fine. I'm wondering if the bricks always have 1 hit point, so always die right away. I think the 2.5-sec Destroy is really a hidden coroutine attached to the brick. So a dead brick can't destroy the explosion.

Try giving the bricks 2-HP to test (should destroy the puff on first hit.) If that's the problem, maybe move the OnCollisionEnter into the ball script. Or just move the explosion spawn there.

---- answer #2 -----

From that last test (not printing the name after a spawn,) go is clearly broken. You're getting a null reference for go, which means the destroy command is destroying nothing. Specifically, Instantiate is spawning just fine. But the as GameObject conversion doesn't work, so just gives you a null (just tried it.)

Fix#1: redeclare particlePrefab (up top) as a GameObject (it's a Transform now)

Alternate fix#2:

Leave particlePrefab as a Transform, but change spawn code to:

 Transform go = Instantiate(...) as Transform;
 Destroy(go.gameObject, 2.5f);

Either way, the GameObject's or Transforms all have to match.

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 Reefer · Oct 11, 2013 at 09:58 PM 0
Share

Those have 1 hit point, 2 HP won't do a difference, just tested. It won't destroy that "Particle System(Clone)" objects it instantiates.

avatar image Owen-Reynolds · Oct 12, 2013 at 01:23 AM 1
Share

I still think the code is fine -- I've used that trick plenty -- and that something else is breaking the Destroy (the parens on the if are missing, but doubt that's the problem.)

Try it in Start (Instantiate, Destroy in 2.5.) To be sure, toss in a Debug.Log("spawned :"+go.name);. Should see them get destroyed.

avatar image Reefer · Oct 12, 2013 at 01:52 AM 0
Share

This is what happens on console / screen when I did it like this:

     // Use this for initialization
     void Start () {
         numBricks++;
         ParticleSystem go = Instantiate(particlesPrefab, transform.position, Quaternion.identity) as ParticleSystem;
         Debug.Log ("spawned: "+go.name);
         Destroy (go, 2.5f);
     }

alt text

B/W Bricks are particles. And no, it doesnt print ever on console what it's spawning.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

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

Destroy a Gameobject with a UI Button 3 Answers

The name "Destory" Does not exist in the current context 2 Answers

destroying game object in a specific transform 2 Answers

Destroy by Tag isnt working,Can't Destroy By Specific Tags 2 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