How to stop a sound playing once another starts? (C#)

I have a prefab for an object (let’s call it chord object) that plays a looping musical chord sound as the player passes through it (on collision). When the player collides with another of these objects, a new chord is played. What I’d like to know how to do is check if a chord loop is already playing and if so, stop that sound playing so that only one chord object (the most recent chord object the player has collided with) plays a sound at a time. I’m using C# and my script attached to the chord object looks like this (it updates the attached audio source component). Currently chords will play back on top of each other in a hideous cacophony, because there’s nothing telling them to turn off:

void OnTriggerEnter(Collider other)
{
//Debug.Log (x);

	if (other.gameObject.tag == "collisionTank") {//If tank collides with this

		Debug.Log ("Play chord");
		chordSource.Play ();	
		
	    }
}

I suggest having an AudioSource reference stored somewhere called currentAudio that is accessible from each of the audioSources’ scripts.

Like an emtpy GameObject that has a script attached where a public AudioSource currentAudio exists.

Then you add a reference to that script into the prefab’s script and write something like:

public AudioManager otherScript;

void OnTriggerEnter(Collider other)
{
    if(other.transform.tag == "collisionTank")
    {
        if(otherScript.currentAudio != null) // If we already have a chord playing..
        {
            otherScript.currentAudio.Stop();  // stop the currently playing Chord and..
            chordSource.Play(); // start the new one, that THIS object has attached..
            otherScript.currentAudio = chordSource; // tell the logic that THIS is playing!
        }
        else
        {    //If this is the FIRST collider we entered..
            chordSource.Play(); // ..start the chord, that THIS object has attached..
            otherScript.currentAudio = chordSource; // ..assign the audiosource for later!
        }
    }
}

And in the AudioManager script you would have:

public class AudioManager : MonoBehaviour{
    public AudioSource currentAudio;

    void Start()
    {}
}

I hope this is what you were looking for, it just stops the current audio and immediately starts the next one.
You could also fade out quickly, altough this might also sound horrible for a split second since they are chords.

PS: Since you are using Prefabs, I would recommend calling the AudioManager’s GameObject “AudioLogic” or something thats easy to identify and at the start of the prefab add:

public AudioManager otherScript; //this doesnt need to be assigned by you anymore
void Start()
{
    otherScript = GameObject.Find("AudioLogic").getComponent<AudioManager>();
}

This automatically finds the AudioLogic GameObject and gets the AudioManager script from it, so you don’t have to assign it for 100 Prefabs by hand. Also when using Prefabs, it would not work to save references they have in the inspector.