Playing audio clip

Please help, i'm simply trying to play an audio clip (1 second) when the player gets a pickup, but no matter what I try it's not working.

I tried audio.Play(); from the documentation but I can't figure out how to use it, it doesn't say. I tried:

var pickup : AudioClip;

function OnTriggerEnter(otherObject: Collider) {

if(otherObject.gameObject.tag == "Mouse") {

    audio.Play();
    MouseScript.playerPoints += 100;
    Destroy(gameObject);

}

}

I tried replacing audio.Play() with a bunch of stuff from the documentation (like PlayOneShot) but it really doesn't make it easy.

There are two common methods for triggering sounds from script.

The first common method to achieve this would be to not have an audio clip variable at all, but instead attach an AudioSource component to your GameObject (or to the pickup object), and drag the "pickup" audio clip reference into that audio source component.

You could then trigger the sound by calling

audio.Play();

from a script on the same object. Or, if you placed the AudioSource on the pickup object, you could call it from your player object when it's collected by using the "otherObject" variable in your OnTriggerEnter function, like this:

otherObject.audio.Play();

However, there is another way to play sounds which doesn't require an AudioSource component to be attached. If your script has a reference to the audioClip to play (as it does currently), a simple way to trigger the sound is to use PlayClipAtPoint.

You could place this code in your OnTriggerEnter function, near the line where the pickup is destroyed:

AudioSource.PlayClipAtPoint(pickup, transform.position);

This will play the sound at the position of the player object (the object that this script is placed on) in the 3D world. Depending on your situation you may want to change this to:

AudioSource.PlayClipAtPoint(pickup, collider.transform.position);

(to play the sound at the location of the other object)

or

AudioSource.PlayClipAtPoint(pickup, Camera.main.transform.position);

(to play the sound at the camera's location, so that it doesn't sound as though it's coming from any particular location in the 3d world)

Audio clips don't play themselves, they are simply the data that an Audio Source can play back. Think of it like trying to render a texture without having a mesh to put the texture on.

See this: http://unity3d.com/support/documentation/Components/class-AudioSource.html

The easiest way to just "play this clip" is to use AudioSource.PlayClipAtPoint.

http://unity3d.com/support/documentation/ScriptReference/AudioSource.PlayClipAtPoint.html

However, if the audio clip you specified is a 3D sound, the sound won't "follow" the mouse object. You could get around this by putting an audio source as a child of whatever is moving around, and playing the audio clip on that. Or you could turn off the 3d property on the audio clip to make it always play at the same volume regardless of the audio listener's position (usually on the camera).

//basic kill object script!!!
//var Other : GameObject;
function Start () {
}
function Update () {
}
function OnTriggerEnter(other:Collider)
{
audio.Play();
Destroy(gameObject);
}

Use AudioSource.enabled property to enable a sound which does not have “Play on Awake” checked in Editor.

i have been working on a script for switching sound an i have been geting one error tell where i went wrong my script

public var SPEAKERRIGHT : Audiosource ;
public var SPEAKERLEFT : Audiosource ;

function Start () {
SPEAKERRIGHT.Audiosource.enable=false;
SPEAKERLEFT.Audiosource.enable=false;
}

function Update () {

if (Input.GetKey(KeyCode.p));
{
SPEAKERRIGHT.Audiosource.enable=false;
SPEAKERLEFT.Audiosource.enable=true;
}

if (Input.GetKey(KeyCode.o));
{

SPEAKERRIGHT.Audiosource.enable=true;
SPEAKERLEFT.Audiosource.enable=false;

}
}

//basic kill object script!!!

//var Other : GameObject;

function Start () {

}

function Update () {

}
function OnTriggerEnter(other:Collider)
{
audio.Play();
Destroy(gameObject);
}
// this is the script i was basic as hell and you can add in needed!**

I am having terrible trouble getting a 1 sec sound to emit when picking up items using the Oculus OVR PC. I cannot get my OVR player controller script to show the variable in the inspector to set the audio clip value, It is not set as MonoBehavior, and I don’t know that I can change this if this is why the public variable does not show up in C#. I see other variables displayed for this script, and unclear how they appear in the OVR script (if I copy and create new public float vars the same way, they don’t show up in the inspector). I have no trouble setting up the variable in the inspector in Javascript or C# (if using MonoBehavior) for other scripts.
So seems I need a way to get the audio source variable to show up in the inspector using the OVR Player Controller or find another way to set the audio source?
Also, another way to setup the pickup sound could work. I tried using another routine, but appears my object deactivation script used to pick up the items is capturing the collision event, and I can;t catch it using the tag with a second separate script.
Any suggestions are appreciated!

Just incase someone else just wants to simply play sound in c#, this works for me:

AudioSource audio = gameObject.addComponent();
audio.PlayOneShot((AudioClip)Resources.Load(“clank1”));

The issue in that code is the object is deleted before the sound can play.

Assuming you have added the audio clip and script as components to your game object, you can use:

GetComponent<AudioSource>().Play();

@Sam Bigos
Maybe it will be a bit late… but the solution i found is:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour {
public AudioClip otherClip;

IEnumerator Start() {
    AudioSource audio = GetComponent<AudioSource>();

    audio.Play();
    yield return new WaitForSeconds(audio.clip.length);
    audio.clip = otherClip;
    audio.Play();
}

}

2.Drag the audio clip that you create in hierarchy to ‘otherClip’ bar in inspector.