- Home /
Play audio when destroying gameobject without removing sprite
I want to play the audio clip when the object gets destroyed. I tried making render sprite false and then play the audio. But I want the object to be completely destroyed as disabling sprite doesn't remove the obstacle and it interferes with my player movement and on collision it initiates gameover. Is there any way I can do this?
Here's my script
void OnTriggerEnter2D(Collider2D bam){
if (bam.gameObject.tag == "Bullet") {
Destroy (gameObject);
}
I figured it out! Just use
AudioSource.PlayClipAtPoint(clip, new Vector3(0,0,0));
Answer by TanselAltinel · Nov 03, 2017 at 06:04 PM
You can use a coroutine and also disable your collider:
private IEnumerator PlaySoundAfterDisable(){
GetComponent<Collider2D>().enabled=false;
GetComponent<SpriteRenderer>().enabled=false;
audio.Play();
yield return new WaitForSeconds(audioClip.length);
Destroy(gameObject);
}
and call your coroutine from trigger enter:
StartCoroutine(PlaySoundAfterDisabled());
Remember, this code is only for giving you an idea and you should edit it for your needs.
Your answer
Follow this Question
Related Questions
Problem with playing sound on Trigger-Enter 2 Answers
How to get decibel's value of game's sound? 0 Answers
Get audio to just play once 1 Answer