- Home /
This question was
closed May 16, 2018 at 09:38 PM by
unity_2020higginbothamdarby.
Question by
unity_2020higginbothamdarby · May 15, 2018 at 09:27 PM ·
c#gameobjectaudioaudiosourcevalues
Volume Control with Slider
Im trying to create a script that sets the game volume to the value of a slider but I can't figure it out. If you can help please do.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SettingsScript : MonoBehaviour {
public GameObject MusicSlider;
public GameObject Cancel;
public GameObject Audio;
public float MVS;
public Camera Cam1;
public float volume;
// Use this for initialization
void Start () {
Cancel = GameObject.Find ("Cancel");
Audio = GameObject.Find ("Audio");
Cam1.GetComponent <AudioListener> ();
Cam1.GetComponent <AudioSource> ();
MusicSlider = GameObject.Find ("MusicSlider");
MusicSlider.GetComponent<Slider> ();
}
// Update is called once per frame
void Update () {
//Get Slider value and assign it to float MVS
Slider MVS;
//Set AudioListnerVolume to MVS
AudioListener.volume = MVS;
}
}
Comment
Answer by oStaiko · May 15, 2018 at 09:34 PM
What you're looking for here is an Audio Mixer https://docs.unity3d.com/Manual/AudioMixer.html
Just set the volume on this mixer group to the slider value. Using this, you can have seperate sliders for sfx, bgm, voices, etc.
@oStaiko Alright but im having some difficulty with assinging the slider value to the audio mixer volume.
Nvm I just found out