x


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{

        }
more ▼

asked Jan 07 '12 at 07:42 PM

cman19 gravatar image

cman19
16 2 4 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
}
more ▼

answered Jan 07 '12 at 08:39 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5086
x148
x14

asked: Jan 07 '12 at 07:42 PM

Seen: 696 times

Last Updated: Jan 07 '12 at 08:39 PM