Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can I mute the background music from a random scene? C#

Discussion in 'Scripting' started by EpicAirBag, Oct 28, 2015.

  1. EpicAirBag

    EpicAirBag

    Joined:
    Sep 16, 2015
    Posts:
    7
    I made a gameobject with a DontDestroyOnLoad function on it in my splash, and I inserted the audio in it, now on my main menu I made a mute button, but I can't figure a way to make the background music stop or mute, is there anyway to solve this?
     
  2. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    If the game object has an AudioSource component, you could try this.
    Code (CSharp):
    1. AudioSource.Stop();
    Or if you just want to mute the volume...
    Code (CSharp):
    1. AudioSource.volume=0;
    Edit:
    Also, if you have multiple AudioSources on the game object and don't have a variable referencing each AudioSource, you could do something like this to stop/mute.
    Code (CSharp):
    1. GetComponents<AudioSource>()[yourNumber].Stop();
    2. GetComponents<AudioSource>()[yourNumber].volume=0;
     
    Last edited: Oct 28, 2015
  3. EpicAirBag

    EpicAirBag

    Joined:
    Sep 16, 2015
    Posts:
    7
    Nvm, I found a way by making the volume of the AudioListener to 0 and making it to 1 to mute and unmute.