playing an audio clip before destroying object

Hi, I have the following script assigned to the key object. I want it to play an audio sound before I destroy the object, but it doesn’t play the audio.

Do I have to delay destroying the object until the audio has completed? I hope not as it means the key will exist in the scene before disappearing, unless of course I just disable the game objects mesh until the audio finishes?

I am not really sure on the best way to do this, any ideas?

using UnityEngine;
using System.Collections;

    [RequireComponent(typeof(AudioSource))]

public class ScriptObjectKeyPickup : MonoBehaviour {

    ScriptPlayerOneManager script;
 
	// Use this for initialization
	void Start () {
    script = GameObject.Find("GameObjectPlayerOne").GetComponent<ScriptPlayerOneManager>();
	}
	
	// Update is called once per frame
	void Update () {
    
	}

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "GameObjectPlayerOne")
        {
            audio.Play();
            script.playerInventory.playerOneKeyAmount += 1;
            Destroy(gameObject);
        }
    }
}

You’ve got the right idea, generally. Destroying an object playing a sound will stop the sound.

Some suggestions:

Destroy the components you don’t want anymore immediately (renderer for example) and use the optional delay parameter on the Destroy call to destroy the gameObject itself after the audio has finished (you can get the length of the clip with audio.clip.length, or just give it a few seconds if you like).

Have a separate object that plays the sound - you could enable it, call a script function that plays a sound or instantiate a whole SFX prefab if you wanted - though if you’re instantiating it, you might as well use the built-in AudioSource.PlayOneShot().

The sound is not played because you destroy the game object and there’s nothing to play the sound.

Deactivate the mesh and the collider, play the sound and destroy the gameObject…

use:

yield WaitForSeconds (audio.clip.length)

To yield while the audio finishes and then destroy the object…

Thanks for the replies, that makes total sense.

So I figure than an easy way for me to do this might be to have the audio on the player, and just trigger the audio on the player before I destroy the object. Problem is I cant make it work in C#

Here is the player code

   [System.Serializable]
    public class soundEffects
    {
        public AudioClip key;
    }

I have assigned the audio clip in the inspector

Then in the monoscript for the player

public soundEffects playerSounds = new soundEffects();

Then on the key object, use the following script:

using UnityEngine;
using System.Collections;

    [RequireComponent(typeof(AudioSource))]

public class ScriptObjectKeyPickup : MonoBehaviour {

    ScriptPlayerOneManager script;
 
	// Use this for initialization
	void Start () {
    script = GameObject.Find("GameObjectPlayerOne").GetComponent<ScriptPlayerOneManager>();
	}
	
	// Update is called once per frame
	void Update () {
    
	}

    private void OnCollisionEnter(Collision collision)
    {

        if (collision.gameObject.name == "GameObjectPlayerOne")
        {
            script.playerInventory.playerOneKeyAmount += 1;
            script.playerSounds.key.Play();
            Destroy(gameObject);
        }
    }
}

It doesn’t seem to like that Syntax of Play(); though. It gives me an error of CS1061: Type ‘unity engine.AudioClip’ does not contain a definition for ‘audio’ and no extension method ‘audio’ of type ‘unityengine.AudioClip’ could be found