Collect Audio Is Not Playing

I’m trying to get a collect sound to play when a coin is collected. When the collect sound is in the coin prefab, the sound doesn’t play, but when the collect sound is outside of the coin prefab, it does. but having the sound outside of the prefab means that I have to set the sound in every individuals coin. Any suggestions as to how I can get the audio to play with the audio source in the prefab? Here’s my code:

    public AudioSource audioSource;       
    public AudioClip collect;
    public float vol = 1;
    void OnTriggerEnter(Collider other)
    {
        ScoringSystem.theScore += 1;
        Destroy(gameObject);
        audioSource.PlayOneShot(collect, vol);
    }

The issue is because you’re destroying the object. As soon as the object gets destroyed, so does the audio source.

There are lots of different ways to solve this but the simplest is probably to use play clip at point.

If you use play clip at point instead of play one shot, And maybe call it before The destroy line, this will play a sound that lasts until after the object it’s on is destroyed. It does this by making a temporary audio source so you wouldn’t need to have one on the object.

for a more in depth description on play clip at point try my article: How to play audio in Unity (with examples)

hope that helps.