- Home /
Destroying object without interrupting audio source?
I want my rocket to be destroyed when it collides with something but I also want my explosion sound playing when it collides. Since an audio source gets destroyed along with the object when you destroy that object, you can't maintain the explosion sound. Any ideas? Thanks.
Answer by robertbu · Dec 09, 2013 at 03:44 PM
How about something like:
collider.enabled = false;
renderer.enabled = false;
Destroy(gameObject, 2.0);
This code hides the game object, disabled the collider, and waits 2.0 seconds to do the Destory(). You will need more code if there are child game object that are visible or have colliders.
I totally forgot about this question while waiting for moderator's approval. So I simply created an audio source within an game object then I sent a message to this object to play given audio right before rocket gets destroyed. But this one seems less complex. Thank you.
Answer by guitarxe · Dec 09, 2013 at 04:22 PM
I had a problem with something similar. I solved it by doing the following. Rather than using the usual AudioSource.Play()
, I used AudioSource.PlayClipAtPoint()
.
You provide the SoundClip to play, as well as the position to play it at in the form of a vector. You can use the rocket's own transform position for that. Then, you Destroy()
the rocket.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Destroy Scripting Help 1 Answer
How to apply OnMouseDown on sprites according to z position... 3 Answers
Ignoring collisions with clones 3 Answers
Object wont destroy on collision with another object 1 Answer