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{

 	    }

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