- Home /
Disable all sounds in Game except background music
I have sounds attached to various game objects in my game and background music to one game object. I want to disable those sounds(like collision sounds,cry sound,shouting sound) but want to play background music continuous. Anybody knows how to do it?
I tried to deactivate camera listener but it mutes all sounds with music.
Answer by louisdeb · Jan 31, 2013 at 01:43 PM
Before each of your statements to play sounds put an if statement checking for a global bool (eg public bool playSound;) which you just change based on whether you want the sounds playing. Don't put the if statement in front of the background music or if you want to disable that as well at any point then use a different bool variable (eg backgroundMusicPlay)
Answer by Punkjim420 · Jan 31, 2013 at 12:27 PM
in javascript,
if you want, one method(maybe not the best) would be to add an if statement in all of the objects you want to mute.
function Update() {
if(yourMainScriptsName.muteNow)) {
audio.mute = true;
}
else{
audio.mute = false;
}
but you have to set the boolean "muteNow" in your mainScriptName to static
static var muteNow : boolean = (trueorfalseyourchoice);
minus the ( ) around your choice.
If I use playerPrefs variables will it harm working on mobile devices? And using it I can save changes permanently.
Answer by the_pheNom · Jun 07, 2014 at 12:06 PM
i have similar function
i am able to disable button sound perfectly, while having my bg music playing
however, how do i do the opposite. have my button sounds play while i disable the bg music
and is there a way to stop an audio file tht is currently playing ( not by reducing the volume of audiolistener, cause i need other sounds like button sounds to keep going)
Answer by screenname_taken · Jun 07, 2014 at 12:20 PM
The way i do it is, i have two floats saved in player prefs. one for music volume and one for sound effects volume. Whenever i trigger an sfx to play, i first check the volume and then play the clip with that volume. So i just have to change the float in player prefs to quiet one or the other or both.
hmm.... the problem im facing is
my scene starts and a bg music plays... all buttons have sounds to it wen clicked or tapped
if i mute sounds... the bg music play -> which is wat is i want
but wen i mute music... i need to stop the bg music and enable the button sounds... i could use ur suggestion and have playerprefs for sound n music volume
but once the music starts playing i cant seem to edit or stop tht music. it plays fully ... i need a cmd to stop the bg music immediatly... NOT LOWER THE VOLU$$anonymous$$E/DISABLE AUDIOLISTENER
hope i made sense!! :/
Just use audio.Stop(); on the component that has the sound you want to stop playing. And i wasn't disabling the audio listener.
ty :) was making a stupid error and complicating everything in my head... ur comment helped.. TY :D
Answer by MrWy07 · Dec 22, 2021 at 12:36 PM
I know I'm late, but if someone's looking for a solution here's mine: Loop through all AudioSources and disable them if there are different of your background music.
foreach(AudioSource i in GameObject.FindObjectsOfType<AudioSource>()) {
if(i != music) {
i.volume = i.volume > 0 ? 0 : 1; // or just i.voume = 0 or something
}
}
Your answer
Follow this Question
Related Questions
Trigger Start of Sound 1 Answer
Sound effect doesn't play correctly 1 Answer
Sound gets loud in while playing game? 1 Answer
Can I use SoundCloud music for my game or showcase ? 0 Answers