Audio Trigger Script Not Responding

Hey guys,

I’m currently working on a rhythm game where if an obstacle is destroyed it makes a clapping sound. However, for some reason the clapping audio doesn’t play. I know the audio works since it plays if I check the PlayOnAwake box and the Debug.Log goes through just fine. Despite this, the audio still doesn’t play when an obstacle is destroyed.

Here’s the script in question:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyObject : MonoBehaviour {


    ScoreSystem Score;

    int ScoreFromHit;

    public AudioSource source;

    private void Start()
    {
        Score = FindObjectOfType<ScoreSystem>();

        source = GetComponent<AudioSource>();

    }

    void OnTriggerEnter( Collider other )
    {


        if (other.gameObject.tag == "Perfect Hit")
        {
            source.Play();
            Debug.Log("clap");

            ScoreFromHit = 25;

            Score.Scoring(ScoreFromHit);

            Destroy(gameObject);

        }

        if (other.gameObject.tag == "Early Hit")
        {
            source.Play();
            Debug.Log("clap");

            ScoreFromHit = 10;

            Score.Scoring(ScoreFromHit);

            Destroy(gameObject);


        }

        if (other.gameObject.tag == "Late Hit")
        {
            source.Play();
            Debug.Log("clap");

            ScoreFromHit = 5;

            Score.Scoring(ScoreFromHit);

            Destroy(gameObject);

        }

    }
}

Any assistance is much appreciated. Thank you!

Is it perhaps a 3D sound and it is physically placed too far away from the AudioListener?

It does start to play. You destroy the gameobject before you can hear it play. The Audiosource is attached to the gameobject. The Audiosource is also destroyed.

Seen a lot of threads like this.

Use an Audio Manager instead. Objects don’t need to handle Audiosources.

Oh good eye Hikiko… I saw source was public and didn’t even think to check for a GetComponent…very nice spotting!

And for kendo, Destroy() has a second optional argument that tells how long to wait:

https://docs.unity3d.com/ScriptReference/Object.Destroy.html

You could delay the Destroy(), but keep in mind the entire thing still sits there, so you probably want a finer-grained solution.

Thanks for the advice. Tried setting up an audio manager but my main issue I’m having right now is how do I write a statement where the audio manager knows when an obstacle is destroyed to play the sound effect?