- Home /
connecting my audio volume to a slider
i seem to have a simple logic error. i'm trying to attach a 2d audio clip to a slider to control the volume but i'm having no luck. the slider is being created after a press of the button here is my code :
var buttonPressed = false;
var volume : float = 0.0;
var musicslider : float = 0.0;
audio.volume = musicslider;
function OnGUI () {
if (GUI.Button (Rect (300, 200, 80, 40), "volumeControl"))
{
buttonPressed = true;
}
if(buttonPressed == true)
{
musicslider = GUI.HorizontalSlider (Rect (300, 200, 100, 40), musicslider, 1.0, 10.0);
}
else{
}
Answer by syclamoth · Jan 07, 2012 at 08:39 PM
Well, this GUI code is all very well, but it doesn't actually change the volume of the Audio Source! As it is, you automatically set audio.volume = musicslider in the Awake function, but it never gets set after that, so the slider does nothing!
You need to add an 'Update' function to apply the changes made in 'OnGUI'.
function Update()
{
audio.volume = musicslider;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613062747im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Referencing variables / functions on another script 1 Answer
Difference between while loop and dowhileloop? 1 Answer
Playing an Animation that is within a GameObject 1 Answer
How do I call a function in another gameObject's script? 5 Answers