- Home /
Question by
TubeSocSamurai · Sep 01, 2014 at 10:48 PM ·
audiopausevolume
Changing Audio volume thru another code
here's my code public float audioVol;
void Start(){
showGui = false;
isPaused = false;
audioVol = GameObject.Find ("BG").audio.volume;
}
void Update() {
if (Input.GetKeyDown (KeyCode.P) && !isPaused) {
Time.timeScale=0.0f;
audioVol=audioVol-0.4f;
isPaused=true;
showGui=true;
}
``im not having any errors but the problem is that the volume isn't decreasing when the game is paused how would i go about fixing this??
Comment
Answer by Ralph1989 · Sep 01, 2014 at 10:55 PM
Thats because you are reading the volume and storing it in start. but you never set the volume in the update.
This is what you want todo. Did not tested the code
public class MyClass : MonoBehaviour
{
AudioSource audio;
void Start(){
showGui = false;
isPaused = false;
audio = GameObject.Find ("BG").audio;
}
void Update() {
if (Input.GetKeyDown (KeyCode.P) && !isPaused) {
Time.timeScale=0.0f;
audio.volume -= 0.4f;
isPaused=true;
showGui=true;
}
}
}
i tried it but nothing happens no errors but it also did not change the volume
Answer by kacyesp · Sep 01, 2014 at 11:34 PM
The problem is that you pause the game as soon as you lower down the volume the first time, and then you never go back into the if statement because of "!isPaused". One solution might be to use another input to lower the volume.
public class MyClass : MonoBehaviour
{
AudioSource audio;
void Start(){
showGui = false;
isPaused = false;
audio = GameObject.Find ("BG").audio;
}
void Update() {
if (Input.GetKeyDown (KeyCode.P) && !isPaused) {
Time.timeScale=0.0f;
isPaused=true;
showGui=true;
}
if ( Input.GetKeyDown(KeyCode.L) )
audio.volume -= 0.4f;
}
}