Question by
DerCain · May 04, 2020 at 09:55 AM ·
arrayaudiosourcefor-loop
Changing the volume of multiple AudioSources in an array
i wrote a script to controll multiple audio sources with one slider. if i change the musicVol or soundVol before the start method the volume of all sources changes according to my value, but if i use the slider of my options menu it seems that the volume doesnt get updated. i am a bit clueless where the mistake hides in this one...maybe someone sees the error...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Options : MonoBehaviour
{
public AudioSource[] musicSources;
public AudioSource[] soundEffects;
public float musicVol;
public float soundVol;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < musicSources.Length; i++)
{
if(musicSources[i].volume != musicVol)
{
musicSources[i].volume = musicVol;
Debug.Log("volume changed");
}
}
for (int i = 0; i < soundEffects.Length; i++)
{
soundEffects[i].volume = soundVol;
}
}
public void SetMusicVolume(float mVol)
{
musicVol = mVol;
Debug.Log(mVol);
Debug.Log(musicVol);
}
public void setEffectsVolume(float sVol)
{
soundVol = sVol;
Debug.Log(sVol);
Debug.Log(soundVol);
}
}
Comment