Unity Unable to Reassign Audio Clip

Hey All,

I am using one audio source throughout all my game levels that handles playing music for my game, which uses DontDestroyOnLoad();. The code I have posted below attempts to switch the audio clip when it is needed from one track to another, but for some reason it just isn’t working. I’ve been looking at this for hours trying to figure out some hack, but I can’t figure it out.

var menuAudio : AudioClip;
var battleAudio : AudioClip;

// Set the lastLevel variable to a value that will force the menu music to start
// playing at the beginning of the game.
private var lastLevel : int = 10;

// Called every time a new level loads by a game object that exists individually
// in each scene.
function CheckLevel () 
{
// Should the menu track be playing right now?
if (Application.loadedLevel < 2)
{
	Debug.Log("Is Menu Level");
	
	// Are we coming from a battle level? If so, start playing the menu track.
	// This makes sure that the track doesn't restart and continues playing
	// 		through menu scenes.
	
	if (lastLevel >= 2)
	{
		gameObject.audio.clip = menuAudio;
		gameObject.audio.Play();
		Debug.Log("Play Menu");
	}
}
// Should the battle track be playing right now?
else if(Application.loadedLevel >= 2)
{
	Debug.Log("Is Battle Level");
	
	// Are we coming from a menu level? If so, start playing the battle track.
	// This makes sure that the track doesn't restart and continues playing
	// 		through battle scenes.
	if (lastLevel < 2)
	{
		gameObject.audio.clip = battleAudio;
		gameObject.audio.Play();
		Debug.Log("Play Battle");
	}
}

// Set the last level equal to the current level, so it can be referenced next
// time the CheckLevel function is called.
lastLevel = Application.loadedLevel;

}

The weird thing is, that the Audio Source is not set to Play On Awake. So the script works the first time, and it successfully changes the music when needed to the battle track, but when it needs to switch from the battle track to the menu music, it just continues to play the battle music. It even gets to the Debug.Log(“Play Menu”); line, but Unity is just blatantly ignoring the lines that reassign audio. I can put any code into that if statement, and unless it is a Debug.Log, it is ignored.

Any suggestions?

Have you tried calling audio.Stop() before switching out the clips?