• 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
1
Question by derrtyones · Nov 13, 2012 at 03:56 PM · javascriptaudiosoundaudioclip

Stop audiosource clip from playing

Hi,

When a condition happens I play an audio clip but I also want it to stop on a certain condition and play another clip. The problem is that I do not know how to stop this clip from playing. Code:

 if(i == 0){
      i=1;
      AudioSource.PlayClipAtPoint(breathing1, transform.position);
      yield WaitForSeconds(2);
      audio.Stop();
      Debug.Log("stop sound");
 }
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

1 Reply

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

Answer by AlucardJay · Nov 13, 2012 at 06:22 PM

Unfortunately, I don't think you can stop PlayClipAtPoint unless you find it as a gameObject and destroy it. Aldo has covered this topic here : http://answers.unity3d.com/questions/316575/adjust-properties-of-audiosource-created-with-play.html

Is there any reason you have to use PlayClipAtPoint and not a permanent audioSource that you just use audio.Stop(); and change the audioClip ?


Edit :

  • Create an empty GameObject > Create empty

  • Attach the Component Audio > Audio Source

  • un-tick Play on Awake

  • Attach the below script, then drop sounds in the Inspector.

  • press play

  • hit 1 or 2 . The audio should stop, change clip and play the selected sound.

Script :

 #pragma strict
 
 var clip1 : AudioClip;
 var clip2 : AudioClip;
 
 function Start() 
 {
     audio.Stop(); // just incase PlayOnAwake is ticked
 }
 
 function Update() 
 {
     if ( Input.GetKeyDown(KeyCode.Alpha1) )
     {
         audio.Stop();
         audio.clip = clip1;
         audio.Play();
     }
     
     if ( Input.GetKeyDown(KeyCode.Alpha2) )
     {
         audio.Stop();
         audio.clip = clip2;
         audio.Play();
     }
 }

Here is a video on audio source : http://www.unity3dstudent.com/2010/07/beginner-b10-audio-basics/

If you use this method, just make the empty gameObject with the audio as a child of your character, then everywhere the character goes, the sound follows. PlayClipAtPoint is usually to play a ricochet sound at the point the bullet hit an object, or other similar scenarios where you only want one sound at a position to play once only (then gets destroyed, pretty much the same as PlayOneShot).

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 derrtyones · Nov 13, 2012 at 06:46 PM 0
Share

Well it's a first person game and I want the sound to play at the character. Not that it plays and once I walk away the sound stays at that specific position. I cannot attach it to an object and destroy it because I want to be able to use these sounds more often. Furthermore I'm quite new to Unity and I am not sure what you mean with the permanent audioSource.

avatar image AlucardJay · Nov 14, 2012 at 05:52 AM 0
Share

Check the edit of my answer, hopefully this and the video will clear up Audio Source.

also the Unity docs :

http://docs.unity3d.com/Documentation/Components/class-AudioSource.html

http://docs.unity3d.com/Documentation/$$anonymous$$anual/Sound.html

The unity3Dstudent site is very useful, and all the videos are under 5 $$anonymous$$utes each.

Here are some links I strongly suggest to all new users :

Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/

Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/

this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp

the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials

A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html

Helpful page with information on using Built-In Arrays and Lists :

http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )

http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

Is there a way to create a random Audiosource loop? 2 Answers

Make the audio manager select from an array of sounds 1 Answer

"Cannot get/set data from streamed sample" from audioclip 1 Answer

Can't get a sound to loop 2 Answers

PlayOneShot returns false for isPlaying 5 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