Crossfade Audio Problem

Okay, så basically I am trying to make a crossfade for my soundtracks, and I stumbled into a small but annoying problem.

As I can only play one soundtrack at the time on a single SoundSource I added two soundsources on the same gameObject (audioSource and audioSourceTwo). I then try to substract from one while adding to the volume of the other when they crossfade for a linear crossfade, which is fairly simple. However, the problem was that it did not work, and I coulnd’t quite figure out why.

After trying to print the volume of audioSource and audioSourceTwo I found them to be the same at all time, as well as the audio.volume was the same as well. Which indicates that the volume slider you see in the inspector is not even considered when you are scripting for it.

Any suggestions would be nice :slight_smile:

if(audioSourceTwo.volume > 0){
				audioSourceTwo.volume = Mathf.Lerp(audioSourceTwo.volume, 0.0f, 0.25f * Time.deltaTime);
			}
			if(audioSource.volume < 1){
				audioSource.volume = Mathf.Lerp(audioSource.volume, 1.0f, 0.25f * Time.deltaTime);
			}

Can you confirm that the two audio sources are different objects? Debug.Log(audioSource != audioSourceTwo);

Strange, they appear to be the same, however, I specifically (multiple times) dragged each to their own respective public variable.. Any way I can differ between in some lines of code instead?

I'm wondering if both AudioSources are on one GameObject, and each time you've been dragging the same GameObject into the inspector field. Maybe there's something weird with your workflow?

1 Answer

1

Nvm I fixed it :slight_smile: turned out I had asked both my audiosources to be = audio; in the start function, so of course they were the same :slight_smile:

Thanks tho