- Home /
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.
Answer by duck · Mar 04, 2010 at 05:43 PM
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)
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.
Answer by Tetrad · Mar 04, 2010 at 05:44 PM
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).
Answer by rileyxfactor · May 19, 2012 at 05:19 PM
//basic kill object script!!!!!!!!!!!!!! //var Other : GameObject; function Start () { } function Update () { } function OnTriggerEnter(other:Collider) { audio.Play(); Destroy(gameObject); }
Answer by $$anonymous$$ · Jun 28, 2012 at 12:32 AM
Use AudioSource.enabled property to enable a sound which does not have "Play on Awake" checked in Editor.
Answer by osayie · Apr 14, 2012 at 08:42 PM
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 na$$anonymous$$g practise!
Your answer
Follow this Question
Related Questions
More than one Sound Collision 0 Answers
audio.Play not working 1 Answer
3D Audio Source plays choppy/poping in web player (Video) 1 Answer
Record Audio for triggering something 0 Answers
Does the number of simultaneous playing audio sources matter? 0 Answers