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

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:

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

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

Solution?

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