• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
15
Question by Sam Bigos · Mar 04, 2010 at 05:24 PM · audio

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

11 Replies

· Add your reply
  • Sort: 
avatar image
68
Best Answer

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)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sam Bigos · Mar 05, 2010 at 12:45 AM 0
Share

Thanks to both of you, really well explained.

avatar image Denis Kniazhev · May 03, 2015 at 12:32 PM 0
Share

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.

avatar image MilosDj · Mar 25, 2017 at 12:04 AM 0
Share

This is good but not complete answer. There is yet another way to play clip:

audio.PlayOneShot(clip); audio.PlayOneShot(clip, volume);

avatar image enoy · Aug 29, 2017 at 09:05 PM 0
Share

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

avatar image
12
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sam Bigos · Mar 05, 2010 at 12:45 AM 0
Share

Thanks to both of you, really well explained.

avatar image
3
Wiki

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); }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daniel G · Jun 19, 2013 at 04:29 AM 2
Share

Enthusiastic coder I can see!

avatar image F-N · Feb 14, 2014 at 07:29 PM 0
Share

trollollol

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;

} }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gregzo · Apr 14, 2012 at 09:23 PM 2
Share

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!

  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple audio recorders like Audio Listener. 0 Answers

Change Editor Window Update Rate 0 Answers

Car engine sound 1 Answer

Unity 5.6 iOS GVR audio spatializer incompatible? 0 Answers

detect audio then record and then play 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges