- Home /
Trying to make a volume slider bar, Part 2
I'm trying to make a slider bar that controls the volume of the background music, and another that controls the volume of the sound effects. I'm working on the music first. I have the slider bar made, but I don't know how to code it to control the music. The background music is an audio file being played from an Audio Source on a gameobject tagged GameController. Here is my code so far.
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour
{
public bool gameIsPaused = false;
public bool buttons = true;
public GameObject gameController;
public GameObject musicVolume;
private float hSliderValue = 0.0f;
void Start()
{
gameController = GameObject.FindGameObjectWithTag("GameController");
musicVolume = gameController.GetComponent<AudioSource>();
}
void Update()
{
if(Input.GetKeyUp(KeyCode.P))
{
if(gameIsPaused)
{
gameIsPaused = false;
Time.timeScale = 1.0f;
} else
{
gameIsPaused = true;
Time.timeScale = 0.0f;
}
}
}
void OnGUI()
{
if(gameIsPaused)
{
if(buttons)
{
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 75, 200, 50), "Resume"))
{
gameIsPaused = false;
Time.timeScale = 1.0f;
}
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - -25, 200, 50), "Settings"))
{
buttons = false;
}
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - -125, 200, 50), "Main Menu"))
{
Application.LoadLevel("MainMenu2");
}
}
if(buttons == false)
{
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - -25, 200, 50), "Back"))
{
buttons = true;
}
hSliderValue = GUI.HorizontalSlider (new Rect (Screen.width / 2 - 60, Screen.height / 2 - -100, 200, 50), hSliderValue, 0.0f, 10.0f);
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - -100, 200, 50), "Music");
}
}
}
}
I thought I could reference the Audio Source by using GetComponent from the GameController gameobject. But I am really rather stuck right now. This code gives me this error
Assets/Scripts/Menu.cs(18,17): error CS0029: Cannot implicitly convert type `UnityEngine.AudioSource' to `UnityEngine.GameObject'
Thanks!
The volume for an audio clip is at object reference.audio.volume (takes a float from 0 to 1))
Awesome! Got it working! Turns out I didn't need to reference the Audio Source directly after all.
I just put
gameController.audio.volume = hSliderValue;
and it works. Thanks!
For anyone interested, here is my final script
using UnityEngine;
using System.Collections;
public class $$anonymous$$enu : $$anonymous$$onoBehaviour
{
public bool gameIsPaused = false;
public bool buttons = true;
public GameObject gameController;
public GameObject musicVolume;
private float hSliderValue = 1.0f;
void Start()
{
gameController = GameObject.FindGameObjectWithTag("GameController");
}
void Update()
{
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.P))
{
if(gameIsPaused)
{
gameIsPaused = false;
Time.timeScale = 1.0f;
} else
{
gameIsPaused = true;
Time.timeScale = 0.0f;
}
}
gameController.audio.volume = hSliderValue;
}
void OnGUI()
{
if(gameIsPaused)
{
if(buttons)
{
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 75, 200, 50), "Resume"))
{
gameIsPaused = false;
Time.timeScale = 1.0f;
}
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - -25, 200, 50), "Settings"))
{
buttons = false;
}
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - -125, 200, 50), "$$anonymous$$ain $$anonymous$$enu"))
{
Application.LoadLevel("$$anonymous$$ain$$anonymous$$enu2");
}
}
if(buttons == false)
{
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - -25, 200, 50), "Back"))
{
buttons = true;
}
hSliderValue = GUI.HorizontalSlider (new Rect (Screen.width / 2 - 60, Screen.height / 2 - -100, 200, 50), hSliderValue, 0.0f, 1.0f);
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - -100, 200, 50), "$$anonymous$$usic");
}
}
}
}