- Home /
Audio on Collision Issue
I am just getting started with working with Unity/Unity Audio, so this may be an easy one.
I have the below script that is attached to a projectile spawning from a turret. I am getting no sound when the visual explosion occurs after hitting the ground. Just to make sure I set this up correctly, I enabled "Play on Awake", in which the audio then played, each time a fireball spawned.
Why is there no sound when "Play on Awake" is disabled (the other parts are the script are executed when the projectile hits the ground)? Thanks.
@script RequireComponent(AudioSource)
var explosion:Transform;
var explodeSound:AudioClip;
function OnCollisionEnter()
{
audio.PlayOneShot(explodeSound);
var explode = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
Wild guess here. $$anonymous$$aybe because you destroy the object that is executing this?
Answer by aldonaletto · Dec 31, 2012 at 01:29 AM
@Lovrenc is right: when you destroy the game object, the audio is killed too. A simple solution is to use AudioSource.PlayClipAtPoint instead - this instruction instantiates a temporary AudioSource that lasts for the sound duration, thus the projectile can be destroyed:
function OnCollisionEnter()
{
AudioSource.PlayClipAtPoint(explodeSound, transform.position);
var explode = Instantiate(...
Another solution is to let the explosion play the sound: add an AudioSource to the explosion prefab, set its Clip to the explosion sound and let Play On Awake checked: this way the sound is played automatically when the explosion is instantiated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090143im_/https://answers.unity.com/themes/thub/images/avi.jpg)