Setting audio volume with horizontal slider (to many AudioSources with different volume)

I have a number of audio sources and they all have different volume. Initial idea was to find all sources with `FindObjectsOfType(AudioSource)` and if slider is set to 0.2, I calculate 20% volume for each with this formula `volume * sliderValue`

But later it appeared that if user sets slider to zero and then tries to set higher volume, volume stays zero. So I understood I need a variable to store initial volume and the formula will be `initialVolume * sliderValue`.

As I didn't find easy way to add a public variable to AudioSource component, I decided to add component AudioVolumeInitial.js (that contains only one line `static var initialVolume = 0.0;`) for each AudioSource with my script (of course I could add this component manually with inspector panel, but that seemed like a hassle to me).

So here is piece of code from my AudioVolume.js. This piece is enough to demonstrate my problem.

function Start () {
    var sources = FindObjectsOfType(AudioSource);
    for (var source : AudioSource in sources) {
        source.AddComponent("AudioVolumeInitial");
        AudioVolumeInitial.initialVolume = source.audio.volume;
        source.audio.volume = AudioVolumeInitial.initialVolume * GUIMainMenu.musicVolume;
    }
}

This line is problematic one so far `source.AddComponent("AudioVolumeInitial");` how do I add component to the parent if all I have is AudioSource components in my array? I tried `source.transform.parent.AddComponent("AudioVolumeInitial");` but it didn't work.

Anyway the best solution would be to find an easy way to get to the parent (that's what I want you to help me with). 2nd option is to attach component AudioVolumeInitial manually (boring, but I can do this if there no other way). And 3rd if I could just add one more public variable to AudioSource component (no idea how, help please).

That's all there is. Thanks.

I managed to fix this with `maxVolume` of the AudioSource, I used it to store initial volume. Couple of extra things appeared to be need to correct volumes, which were set in other scripts. But don't mind that. Main code it this (I also added check for tag to know if it's music or sound effects):

function Start () {
    var sources = FindObjectsOfType(AudioSource);
    for (var source : AudioSource in sources) {
        if( source !== GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) {
            source.maxVolume = source.volume;//set this only on start, we keep initial volume inside maxVolume
            source.volume = source.maxVolume * GUIMainMenu.effectsVolume;
        }
        else if ( source == GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) {
            source.maxVolume = 1;//set this only on start, we keep initial volume inside maxVolume
            source.volume = source.maxVolume * GUIMainMenu.musicVolume;
        }
    }
}

function GlobalVolume()
{
    var sources = FindObjectsOfType(AudioSource);
    for (var source : AudioSource in sources) {
        if( source !== GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) {
            source.volume = source.maxVolume * GUIMainMenu.effectsVolume;
        }
        else if ( source == GameObject.FindWithTag("SoundTrack").GetComponent(typeof(source)) ) {
            source.volume = source.maxVolume * GUIMainMenu.musicVolume;
        }
    }
}

Your problem is probably in line 5. If you set AudioVolumeInitial.initialVolume this won't affect the instance of the component you just created. Instead try this:

AudioVolumeInitial av = source.AddComponent("AudioVolumeInitial") as AudioVolumeInitial;
av.initialVolume = source.audio.volume;

This is C# syntax, I don't use js so I'm not sure about the syntax there but the point is to store the return value of AddComponent as a AudioVolumeInitial component and use that to set the volume.