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.