Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
9
Question by imnickb · Dec 11, 2011 at 03:01 PM · audiosoundstopall

How to Stop all audio

I have a scene with about 20 game objects and when one is clicked, it begins playing audio. When the next on is clicked I need to stop any audio that's playing and play the next audio. Everything I've found regarding this topic says to set Time.timeScale to 0, but all that's going to do is pause everything. The other answer I keep seeing is that I should set AudioListener.volume to 0, which will be essentially muting everything.

Here's what I currently have so far. I just need to insert something to stop all audio before a new track begins. Sorry if this is a simple request, I couldn't find an answer anywhere!

 function Update(){
     if (audio.isPlaying){
         transform.Rotate(.5, .25, .75);
         }
 }
 
 function OnMouseDown(){
     if (audio.isPlaying){
         audio.Stop();
         transform.rotation = Quaternion.identity;
         }
     else { 
         //Stop All Audio
         audio.Play();
           }
 }
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

10 Replies

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

Answer by imnickb · May 07, 2012 at 01:30 PM

If anyone runs across this thread looking for an answer, here's how I ended up doing it. This is from Mortis on the Unity Forums.

 private var allAudioSources : AudioSource[];
  
 function Awake() {
     allAudioSources = FindObjectsOfType(AudioSource) as AudioSource[];
 }
  
 function StopAllAudio() {
     for(var audioS : AudioSource in allAudioSources) {
         audioS.Stop();
     }
 }

Then just call the StopAllAudio() function when you want everything to stop.

Comment
Add comment · Show 5 · 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 Bunny83 · May 08, 2012 at 01:24 AM 1
Share

Feedback from the OP is always welcome. $$anonymous$$ost time people misuse the answer function to post more questions or comments. So this gives me a little bit of hope that not the whole world run out of common sense ;)

+1

avatar image LauraCortes · Mar 24, 2013 at 06:48 PM 0
Share

Thank you, It was what I was looking for

avatar image poegel · Mar 13, 2014 at 11:07 AM 0
Share

Thanks a lot of this!

avatar image exorakhilas · Mar 12, 2015 at 06:49 AM 0
Share

Life saver! Thanks OP (re$$anonymous$$ds me of Reddit somehow).

avatar image amanpu · Aug 17, 2015 at 10:34 AM 2
Share

Ins$$anonymous$$d of initializing allAudioSources in Awake, I would recommend to use some local array variable inside StopAllAudio() method. This will be helpful in the case somebody is assigning component at runtime.

avatar image
17

Answer by dpanov76mail-ru · Feb 25, 2015 at 10:37 AM

C# Version

 //Stop all sounds
 private AudioSource[] allAudioSources;
 
 void StopAllAudio() {
     allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
     foreach( AudioSource audioS in allAudioSources) {
         audioS.Stop();
     }
 }
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 $$anonymous$$ · Aug 13, 2015 at 10:21 PM 1
Share

Excellent and very useful!:D

avatar image mayur3d · Sep 11, 2016 at 12:23 PM 0
Share

Thanks you very much !!!! solved my big problem...

avatar image nickdandruff · Nov 23, 2016 at 04:02 PM 0
Share

i get this error

Assets/Script/Playsound.cs(21,73): error CS0039: Cannot convert type UnityEngine.Object' to UnityEngine.AudioSource[]' via a built-in conversion

avatar image shumila · Dec 14, 2018 at 05:17 PM 0
Share

thank u soo much

avatar image
2

Answer by JmPrsh153 · Jul 01, 2013 at 03:13 PM

http://docs.unity3d.com/Documentation/ScriptReference/AudioListener-pause.html

enough said ;)

 function OnMouseDown(){
     if(!paused)
         {
         Time.timeScale = 0;
         paused=true;
         Guiscript.enabled = true;
         AudioListener.pause = true;
         }
     else
         {
         Time.timeScale = 3;
            paused=false;
            Guiscript.enabled = false; 
            AudioListener.pause = false;
         }
 }

forget the guiscript bit thats just my way of showing my gui when i press the pause button but this method works :)

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 Suduckgames · Jun 13, 2017 at 11:13 AM 1
Share

Take in account that with Time.timeScale = 3 you passing the time x3 times faster, normally you want to be Time.timeScale = 1

avatar image
1

Answer by Nerull22 · Dec 11, 2011 at 05:11 PM

Well what you could do, is put all your audio sources into an array. And then when you want to stop all audio, just cycle through the array with a for loop stopping all the audio sources and then start playing the new audio source.

None of this code is tested, so just use it as a reference.

 private AudioSource arrAudio;
 
 Start()
 {
     //Declare the array here and place your audio sources inside of it.
 }
 
 Update()
 {
     for(var int i=0; i < arrAudio.length; i++)
     {
         arrAudio[i].stop();
     }
 
     arrAudio[i].Play(); //This is to play the new audio source.
 
 }

So try something like that, and see if it works for you.

Comment
Add comment · Show 3 · 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 Bunny83 · Dec 11, 2011 at 06:30 PM 0
Share

The basic idea is good but your code is a bit messy. It's no language i know. I guess it should be C# (due to your variable declaration), but the rest...

  • Your methods always need a return type in C# (void in this case which means nothing is returned).

  • The for loop in Update makes no sense. It should be in the On$$anonymous$$ouseDown event when you want to stop the audio.

  • arrAudio should be an array so it have to be declared as array: private AudioSource[] arrAudio; or to match the OPs language (Unityscript): private var arrAudio : AudioSource[];

  • In your example "i" is only valid inside the for loop so using it outside won't work and doesn't make sense in this case.

avatar image Nerull22 · Dec 11, 2011 at 07:26 PM 0
Share

Like I said, use it as a reference. I was just waking up when I wrote this. And I normally have reference next to me. So I apologize for the sloppy code, but I'm just hoping that the concept did get across.

$$anonymous$$y apologies.

avatar image imnickb · Dec 21, 2011 at 02:13 AM 0
Share

Thanks for the input! I haven't gotten to look at this yet but I'm planning on it tomorrow. Does anyone else have any insight on this?

avatar image
0

Answer by jister · Dec 24, 2011 at 01:28 AM

this turns them on and off still need to find some way to look if there is sound playing and stop that. sadly enough if(audio.isPlaying){audio.stop();} doesn't work :(

     function OnMouseOver (){
 
     if(GetComponentInChildren(AudioSource).enabled == false && Input.GetMouseButtonDown(0)){
 
         GetComponentInChildren(AudioSource).enabled = true;
     
     
     }
     
     else if(GetComponentInChildren(AudioSource).enabled == true && Input.GetMouseButtonDown(0)){
     
     GetComponentInChildren(AudioSource).enabled = false;
     
     }
 
 }
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
  • 1
  • 2
  • ›

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sound stops when object is destroyed 6 Answers

java.lang.IllegalStateException: stop() called on uninitialized AudioTrack. 1 Answer

How to stop Audio not all?? 0 Answers

No Sound at all 12 Answers

how can I play 2D sound from all output devices 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