Playing audio clip

I am a noob to C# and Unity in general, and I am having difficulty trying to play a sound.

So it’s pretty simple, I have a collider object with attached audio source. When it’s clicked, it’s supposed to play a sound and destroy, so I had this:

    	void OnMouseDown()
    		Destroy (gameObject);

The problem with this, the sound wouldn’t play because the object was being destroyed. That makes sense. So I started searching for another method. I found AudioSource.PlayClipAtPoint() doing searches for similar issues. This sounds perfect but I can not get it to work.

So the object I have is a roach, it has the audio clip “splat” attached to it. On the roach script, I have the following code:

using UnityEngine;
using System.Collections;

    public class roachCode : MonoBehaviour {
    public float speed = 30;

    void Start() 
        // Initial Velocity
        GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;

	void OnMouseDown()

		AudioSource.PlayClipAtPoint(splat, transform.position);


I keep getting the error "The name ‘splat’ does not exist in the current context. I have tried creating a gameobject Audio Source to put onto my scene and I named it “splat” but still same problem. I tried changing the “splat” to “roach,” still same issue. I don’t know what I am doing wrong here. How can I get this command to recognize splat?

Thank you for any help you can provide.

EDIT: In the documentation for Destroy, you can have 2 parameters.

  1. the object to destroy
  2. However long to wait before destroying it.


Destroy(gameObject, 1f);

Replace “1f” with however long you want to wait before destroying the object.

Have a public AudioClip in the script and an AudioSource on the object. Fill the AudioClip var with a sound clip in the editor.

audio_Source = GetComponent<AudioSource>();

audio_Source.PlayOneShot ("this is your audio clip", audio_volume);

Just remember that if you Destroy your object before the clip is played or finished playing then it will appear like nothing happened.

Instead of a Destroy(gameobject) use a function that disables certain components during its destroy timer.