- Home /
Sound not playing
Hello I’m having a hard time getting a object that’s getting destroyed to play an explosion sound. I’ve tried multiple ways to get it to play but it doesn’t play the audio comes up as mono idk if that matters it’s also under a OnTriggerEnter2D idk if that would make a difference, their are other sounds playing in the background & when the player shoots... idk what I’m doing wrong
public GameObject Explosion;
public GameObject Player;
public float Speed;
public AudioSource explosionSound;
void Start () {
Player = GameObject.Find(“Player”)
explosionSound = GetComponent<AudioSource>();
}
void OnTriggerEnter2D(Collider2D col)
{
if ((col.tag == “Player”) || (col.tag == “Bullet”))
{
explosionSound.Play();
PlayExplosion();
Destroy(gameObject);
}
Answer by luiscmendez · Jul 10, 2018 at 06:53 PM
The quick answer is that you're playing the sound ON the GameObject that is being destroyed. One frame the sound is playing, and the next you're destroying it.
Rather than using the variable type AudioSource, you'll use AudioClip and spawn a new AudioSource every time you want to play the sound.
Keep in mind this might not be ideal if you plan on creating a specific mix for audio since you'll have to set the audio source settings at runtime.
This may be a good place for you to start: AudioSource.PlayClipAtPoint
Thank you for this information I didn’t know that. How do I go about spawning a new AudioSource?
By using the method AudioSource.PlayClipAtPoint it will spawn a new Audio Source for you. All you need to do is give it the Vector3 location of where you want to play it. Presumably the object that is being destroyed in your situation.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a sound play only once with a Boolean variable in javascript 1 Answer
Door Opens When Not In Trigger And Sound Help 1 Answer
Flip over an object (smooth transition) 3 Answers