set the volume of AudioListener

Hi

Stuck on something small and can’t see what I’m doing wrong. I’m trying to set the volume of the AudioListener on the users camera. Three variables set up as follows, UserLight has the correct light dragged into it in the script panel:

public GameObject UserLight;
AudioListener myAudioListener;
private float VolumeSliderValue = 0.6f;

then I have this in onGUI:

myAudioListener = UserCamera.GetComponent<AudioListener>();
VolumeSliderValue = GUI.HorizontalSlider(new Rect(AlignLeft + 112, 144, 100, 30), VolumeSliderValue, 0.0f, 1.0f);
myAudioListener.volume = VolumeSliderValue;

I’m getting the error:

I’ve also tried:

UserCamera.GetComponent("AudioListener").volume = VolumeSliderValue;

but that gives me the error:

Any ideas what I’m doing wrong here?
Thanks in advance.

That’s because AudioListener and the .volume member are static… you don’t need the myAudioListener variable. Just do this:

AudioListener.volume = VolumeSliderValue;

ah, I had assumed in the examples I was looking at the AudioListener was the variable name. So does mean that there can only be one AudioListener in Unity at any one time? - if not how would you address different ones? This did solve it for me thanks, I’m just curious about whether there can be more than one.

Yep you can only have one Audio Listener at any one time and you’ll actually get error messages if you have more than one. Generally the listener is attached to the main camera or first person. I’m not sure why you’d want to have more than one audio listener… but I suppose if you were switching cameras… you could actually remove your audio listener component from your main camera and attach it to your other camera… or you may (I’m not sure about this one) be able to have more than one audio listener component but only have one active at a time…

If you’re talking about different sounds from different places, then you work with multiple AudioSources but only ever have one Listener. Just think of the Listener as your “ears”. You only have one pair.

right so addressing isn’t an issue in this case, good to know. Many thanks for your help.