- Home /
Destroying a game object with particles waits for end of particle life
I have the following to destroy a game object (missile) which has a particle component:
GameObject energyBolt= Instantiate( missilePrefab, transform.TransformPoint( 0.0f, -0.25f, 5.0f ), Quaternion.identity );
Destroy( energyBolt );
but the missile doesn't disappear from the editor or the play area for a few seconds, which seems linked to the duration of the particles.
Is this a known issue?
Hi rd_mcn, If you use Destroy(missile);
the object should just disappear, no matter if it has particles or not. To clear things out, what is the structure of your missile object? Does it contain the particle system as a child object? Could you give me more info about it so I can help you? Thanks ! :)
Thanks for your response; it should indeed disappear but it is failing to do so for a few seconds. The missile object has the following components: Particle System, Sphere Collider, Rigidbody, Audio Source
$$anonymous$$mm... In your script is missile a GameObject type of variable or it's another type?
It was created with an Instantiate() and the object returned is being stored, when Destroy is called I have a Debug line that shows it being called. The object is destroyed but only a few seconds later.
Answer by rd_mcn · Mar 01, 2018 at 07:51 AM
I have finally worked it out and I think others may find this helpful.
Debug.Break()
runs the statements following it before the game pauses in the editor (possibly to the end of the frame processing). This entailed that the Destroy() had already been completed when the game had paused which led me to following the wrong missile.
D'oh.
If you wish to test it, then you can place a Debug.Break() in your code followed by some other statements, including debug logging and you will see these in your console when the Debug.Break() is executed.
This isn't specified in the documentation that I had a look through and is well worth knowing about.
Thanks again to everyone who helped along the way.
Answer by dishant27 · Feb 28, 2018 at 10:18 AM
The idea is to get the lifetime of the particle and destory the energyBolt after that duration.
Please try something like this after instantiating the energyBolt: float duration = energyBolt.GetComponent ().main.startLifetime; Destroy(energyBolt, duration )
If I'm not missunderstanding the OP, he wants the bolt to disappear even if the particle system is still running :/ and of course, if I misunderstood him, your answer should be a good solution! ;D
Thanks for that; the particles loop around a 5 second duration. The destroy should happen when the GameObject collides with another object; not after just one cycle.
In that case, you can attach a script to missile and write the code in OnEnable method: 'float duration = gameObject.GetComponent ().main.startLifetime; Destroy(gameObject, duration )'. If the gameobject collides with someother gameobject before lifecycle, you can destroy it immediately using OnColliderEnter.
When you call Destroy(gameObject); the object should just be destroyed. That is the main problem here, and we are trying to find out why something that should be happening is not happening. :/
I tried the Clear() function in the particle system but it doesn't solve the issue :-(
But, it's working for me. Can you share the code which you tried with Clear()?
if( Time.timeSinceLevelLoad - missile.timeCreated > timeExpiry$$anonymous$$issiles || missile.energyBolt.GetComponent<EnergyBolt>().destroyed == true )
{
missile.energyBolt.GetComponent<ParticleSystem>().Clear();
Destroy( missile.energyBolt );
}
missile is a structure and the energyBolt is the GameObject.
Try to test it in a separate empty scene without any if condition. Just destroying the gameObject after some seconds using Invoke().
And you say that if you put a Debug.Log inside that if, the line is printed at the same moment it is called but the missile gets destroyed a few seconds later?
Thanks for the advice (apologies for the delay in replying am in Australia and sleep was needed). I am planning on reproducing this in a test project to send as another bug report to Unity; firstly to ensure it is Unity and not something I've done.
Thanks for everyone's help; mostly I just wanted to know if it was a known issue.