x


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.

more ▼

asked Feb 28 '10 at 09:07 AM

Dima Antonov gravatar image

Dima Antonov
64 6 7 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
        }
    }
}
more ▼

answered Mar 01 '10 at 11:29 AM

Dima Antonov gravatar image

Dima Antonov
64 6 7 15

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 28 '10 at 12:43 PM

StephanK gravatar image

StephanK
6k 39 53 93

No, in this everything seemed okay in JS.

Mar 01 '10 at 11:22 AM Dima Antonov
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1026
x74
x24

asked: Feb 28 '10 at 09:07 AM

Seen: 2995 times

Last Updated: Feb 28 '10 at 09:07 AM