Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by evand · Jun 24, 2013 at 12:28 AM · audioontriggerenterstop

How can you stop all other audio sources from playing and then start a new audio source?

This should be really, really easy and I'm ripping my hair out over this. C# language.

My goal is to have two separate triggers used to play a different song when the player (and only the player) walks into them. For an example, imagine a building with 3 rooms; on the left is a room that plays the super mario brothers music and on the right there is a room that plays sonic the hedgehog music. Both rooms have their music activated only by the player when he walks into the room and it CONTINUES to play until another audio source is played, in which case it will stop and be replaced.

Unfortunately my code has it set up to where both songs play simultaneously. I can stop ALL music, but I can't stop one individual track and let another play in the same scene.

I have my music script on the player and have two separate objects being used as triggers when I walk through a doorway. I managed to stop them from repeatedly starting over, but no matter what I do I cannot get one trigger to stop the audio being played prior to it being activated.

I'm using an array to list two songs [0, 1] and can get both songs to play when my player moves onto the trigger, but cannot get them to stop so the next song plays on its own!

audio.Stop(); doesn't work, no matter where I put it or how many times I use it. I've poured over the scripting reference, all of Unity Answers, google, and have watched like 5 audio tutorials and no one really touches on this question.

Code below:

 using UnityEngine;
 using System.Collections;
 
 public class MusicChange : MonoBehaviour {
     
     public AudioClip[] Music;
     
     
     // Use this for initialization
     void Start () {
                
     }
     
     // Update is called once per frame
     void Update () 
     {
         
     }
     
     void OnTriggerEnter(Collider other)
     {
         if(gameObject.tag == "Testing")
             {
             if(audio.clip != Music[0])
             {
             audio.Stop();
         if(other.gameObject.tag == "Player")
             {
             audio.clip = Music[0];
             audio.Play();
             }
             }
             }
         
         if(gameObject.tag == "Overworld")
         {
             if(audio.clip != Music[1])
             {
             audio.Stop();
         if(other.gameObject.tag == "Player")
             {
             audio.clip = Music[1];
             audio.Play();
             }    
             }
             }
 }
 }
 
Comment
Add comment · Show 4
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 SpecticalPro · Jun 24, 2013 at 04:26 PM 0
Share

what is your audio source attached to, the character or the triggers?

avatar image evand · Jun 24, 2013 at 04:32 PM 0
Share

Currently it is set to the player, but I have attempted it to be set on the player, triggers, camera, or a combination of them to no avail.

I can make it to where the sound stops, then immediately starts again (when inside the trigger) or make the song stop with OnTriggerExit, but it's not suited to my needs.

No matter what I seem to try, I can't make a song start when the player walks into collider A and then have that song stop and a different one start when the players walks into collider B.

avatar image SpecticalPro · Jun 24, 2013 at 04:41 PM 0
Share

Try getting audio in the start function and storing it

ie, private AudioSource myAudio;

then in start,

myAudio = audio;

then just use myAudio throughout..

avatar image evand · Jun 24, 2013 at 11:29 PM 0
Share

Unfortunately it doesn't work this way either. :(

A few quirks I've noticed:

1) When my player walks through the FIRST trigger... nothing happens. When he walks into the second, the audio will play for just one blip and then cease.

2) When I changed "$$anonymous$$usic[0]" and "$$anonymous$$usic[1]" to just "audio" in general, I noticed for some reason that the audio didn't play for the first trigger, but played completely for the second!

3) The music that started playing for the 2nd trigger will stop when I enter into the first trigger or any other, including my camera bounds. I went ahead and turned off the camera and played from the scene view and confirmed.

4) I fiddled with the tags to try to figure out more; switched "Testing" / "Overworld" with Player and put it only on the Player. Worked the same as above.

Reading the code I feel it SHOULD be working, I don't know why it isn't! What in my code would cause ANY trigger to stop the audio?

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by LightSource · Jun 24, 2013 at 01:53 AM

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.

Original: http://answers.unity3d.com/questions/194110/how-to-stop-all-audio.html

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 evand · Jun 24, 2013 at 12:31 PM 0
Share

I saw that when searching and tried it, but it was unsuccessful (also in Java as opposed to C#, but what can ya do XD).

avatar image
1

Answer by evand · Jun 26, 2013 at 12:44 AM

I have solved this issue!

When you are trying to activate an individual audio source it AUTOMATICALLY STOPS THE CURRENTLY PLAYED SONG.

When I put in "audio.Stop" or an equivalent, the program's thought process was

" Trigger is right. Trigger song is right. Other Trigger is right. Stop current song and play new song. Stop song. "

    void OnTriggerEnter(Collider other)
     {
        if(gameObject.tag == "Player")
          {
          if(myAudio.clip != Songs[0])
          {
 //         audio.Stop();
 // The above line is what needed to be removed.
                 
        if(other.gameObject.tag == "Testing")
          {
          myAudio.clip = Songs[0];
          myAudio.Play();
          }
          }
          }
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 SpecticalPro · Jun 26, 2013 at 02:47 PM 0
Share

I tried looking through unity docs but to no avail. I think the reason behind this is that when the audioSource doesn't have a clip, the stop() command get queued, then when you tell it to play it automatically stops right after. Unity can be weird sometimes.

avatar image
0

Answer by SpecticalPro · Jun 24, 2013 at 06:07 PM

 using UnityEngine;
 using System.Collections;
  
 public class MusicChange : MonoBehaviour {
  
     public AudioClip[] Music;
     private AudioSource myAudio;
  
     // Use this for initialization
     void Start () {
         myAudio = audio;
     }
  
     // Update is called once per frame
     void Update () 
     {
  
     }
  
     void OnTriggerEnter(Collider other)
     {
        if(gameObject.tag == "Testing")
          {
          if(myAudio.clip != Music[0])
          {
          myAudio.Stop();
        if(other.gameObject.tag == "Player")
          {
          myAudio.clip = Music[0];
          myAudio.Play();
          }
          }
          }
  
        if(gameObject.tag == "Overworld")
        {
          if(myAudio.clip != Music[1])
          {
          myAudio.Stop();
        if(other.gameObject.tag == "Player")
          {
          myAudio.clip = Music[1];
          myAudio.Play();
          }    
          }
          }
 }
 }
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 evand · Jun 24, 2013 at 11:30 PM 0
Share

Same comment as above, just noticed I typed it on the wrong one:

Unfortunately it doesn't work this way either. :(

A few quirks I've noticed:

1) When my player walks through the FIRST trigger... nothing happens. When he walks into the second, the audio will play for just one blip and then cease.

2) When I changed "$$anonymous$$usic[0]" and "$$anonymous$$usic[1]" to just "audio" in general, I noticed for some reason that the audio didn't play for the first trigger, but played completely for the second!

3) The music that started playing for the 2nd trigger will stop when I enter into the first trigger or any other, including my camera bounds. I went ahead and turned off the camera and played from the scene view and confirmed.

4) I fiddled with the tags to try to figure out more; switched "Testing" / "Overworld" with Player and put it only on the Player. Worked the same as above.

Reading the code I feel it SHOULD be working, I don't know why it isn't! What in my code would cause ANY trigger to stop the audio?

avatar image SpecticalPro · Jun 25, 2013 at 07:14 PM 0
Share

One thing I just noticed, and its probably nothing, but the array name "$$anonymous$$usic" is shaded as a keyword, which is weird and may just be something to do with this forum, but due to lack of other ideas try changing this to something else, maybe "clips" or something.

avatar image evand · Jun 26, 2013 at 12:15 AM 0
Share

No dice. :\ Good thinking though.

I've been tinkering more. Turns out when I remove one of the if statements entirely, the remaining if statement will play. I have no idea what in the world would cause it to not work if I have two identical sections of code. The only things that change are the tags and the songs!

void OnTriggerEnter(Collider other) { if(gameObject.tag == "Player") { if(myAudio.clip != Songs[0]) { myAudio.Stop(); audio.Stop (); if(other.gameObject.tag == "Testing") { myAudio.clip = Songs[0]; myAudio.Play(); } } }

     if(gameObject.tag == "Player")
    {
      if(myAudio.clip != Songs[1])
      {
      myAudio.Stop();
             audio.Stop();
    if(other.gameObject.tag == "Overworld")
      {
      myAudio.clip = Songs[1];
      myAudio.Play();
      }    
      }
      } 

}

avatar image evand · Jun 26, 2013 at 12:19 AM 0
Share

HOLY CRAP I FIGURED IT OUT!

Added as an answer.

avatar image
0

Answer by jococo · Aug 18, 2013 at 07:48 PM

Solution?

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 zerodragonheart · Jun 16, 2016 at 06:07 AM

So,, well i found out that maybe all of you have already solved this, but i managed to solve this issue with the following script: You only have to put the AUDIO to stop in the STOP box and the one you want to play in the PLAY box of the inspector. I use this cuss wn the player walks into the TRIGGER SONG it happens.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class MusicSwitch : MonoBehaviour
 {
 
 
     public AudioSource play;
     public AudioSource stop;
 
     // Use this for initialization
     void Start()
     {
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
 
         if (other.name == "Player")
         {
             stop.Stop();
             play.Play();
         }
     }
 }
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

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

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

Related Questions

Stop sound on input.getkeyup 1 Answer

Play Audio on Collision or Trigger Enter 4 Answers

Don't destroy a variable when changing scene 2 Answers

Why isnt OnTriggerEnter Tags working. 1 Answer

How to stop Audio not all?? 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