turn off backgound music

Hello, I have background music playing from an audio source attached to a gameobject. I have a button coming up on the GUI that is supposed to turn off the music. Instead I am receiving the opposite affect of what I want. The scene starts quiet and plays the audio when I press the button. Any help would be appreciated Thanks in advance. Here is the code:

var backPlay : boolean = true;


function OnGUI()
{
  
    if (GUI.Button(Rect(10,70,50,30),"Stop Music"))
    {
      backPlay = false;
    }
    
    if(backPlay)
    {
    audio.Play();
    }
 }

why don’t you just use the control volume for the audio listener ?

AudioListener.volume = 1.0;

and then switch it of by 0.0

it is much more easy as i guess

  1. Script: add audio.Stop(); or audio.Pause(); after or before backPlay = false;

  2. If 1. will not work, make Sure Audio Source is not Muted in Inspector

Try this:

function OnGUI()
{

    if (GUI.Button(Rect(10,70,50,30),"Stop Music"))
    {
      if (audio.isPlaying)
       audio.Stop();
    }
 }

got it working thanks for the help everyone. It is much appreciated :slight_smile: