Playing audio clip (1888)

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.

11 Answers

11

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)

Thanks to both of you, really well explained.

I think that AudioSource.PlayClipAtPoint should be used with care - it seems to create a new audio source any time it's called, which is discarded immediately after playing the sound, so it can easily affect performance.

This is good but not complete answer. There is yet another way to play clip: audio.PlayOneShot(clip); audio.PlayOneShot(clip, volume);

Thank you duck it is more easier or proper for me the third way.

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).

Thanks to both of you, really well explained.

Do you mean the MyComponent class would be abstract in the example, or maybe the MyScriptable class? Or is it some unmentioned other class?

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

Enthusiastic coder I can see!

trollollol

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;

}
}

Post this is a seperate, polite question, and it will be answered! Do not use questions already marked as solved, and read a bit about best variable naming practise!

Did you double click it to show you the actual scriptable object? of course it doesn't get serialized on the normal fashion.

//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!**

The MyScriptable class would be abstract.

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!

to much text to read, sry dude. Try ask it instead of giving it as an answer :)

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.

I did but now the flashlight is floating near the hand. How can i make that the character will hold the flashlight in his hand palm ? I changed the GameObject position to the hand position but it's not holding it it's just floating near the character. Look at the screenshot i added. And i did it all in the Hierarchy i'm not using yet any script. I wonder how to make it to hold the flashlight with the hand palm. ![Flashlight][1] [1]: /storage/temp/92940-ss125.jpg