- Home /
Separate Listeners for Sound FX and Music?
I know that the Unity documentation says that you're only supposed to have one audio listener in a scene, if you strictly have to follow this to have your game work how can you have two parameters for volume on the audio listener, one to alter the sound effects volume and one for music? Or would I just have to directly edit the volume on all of the sfx audio sources and the music audio source (which is very undesirable since that would mean if I wanted a sound to be louder or quieter by default then I'd have to open it in some external audio software). Anybody figured out an easier way to do this than the ways I listed above?
Here's some quick code going off of alucardj's answer if you don't feel like writing it:
//variables that will be needed
var sfx : AudioSource[];
var music : AudioSource;
var volumeSFX : float;
var volumeM : float;
function Update () {
//Sets volume to volumeSFX
for(var i = 0; i < sfx.Length; i++){
//Remember that volumeSFX must be between 0.0 and 1.0
sfx[i].volume = volumeSFX;
}
//Assuming that you only have one music audio source
music.volume = volumeM;
}
Store your 2 types of sounds in 2 separate arrays/lists. Then for change in group one volumes, just cycle through all the items in group one array/list and set the volume to the new universal value for that group.
ok I'll try that, sounds like it'll work just fine though, however there is a tad bit that's kind of wrong with this concerning my needs... but setting the audio source volume seems like the only way to do it
I shall wait for other suggestions as this is a new question. If there are no other suggestions, I shall post in more detail with some example code for new /other readers. Thanks =]
That's great, I just came back to see how this question was doing, and (I assumed anyway) you had no problem writing that code. I was thinking 'I don't remember that code, did I suggest what they had already done?'! That was the idea, and how I would personally do it if stuck with the problem. This comment should serve as a bump at least =]