- Home /
How To Turn Off Sound For Certain Objects?
I have two sound effects right now, one for the jump, and another for the music. I have an option in my options menu to where I can turn off the sound or music. When I turn off sound, everything turns off including the music. So how can I target only certain sounds/objects in the script? So far I use AudioListener.volume = 0; when I turn sound off. EDIT: If you read my latest comment(all the way at the bottom of the screen), you will see that I changed my script a little bit. I now have the music on a different script, on a different gameobject attached to the Player. Here's my music script:
public class MusicTest : MonoBehaviour {
AudioSource audio; //variable
public AudioClip[] songs; //variable
int currentSong = 0; //variable
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
//This is for when I put more music
if (audio.isPlaying == false){
currentSong++;
if(currentSong >= songs.Length)
currentSong = 0;
audio.clip = songs[ currentSong ];
audio.Play();
}
}
}
My Jump sound is the same. Look at the replied comments for the script. I'll re-post it and my menu to turn off sound and music again here when pastebin works again. EDIT2: Okay pastebin is working again, so here's my Jump Audio script(the code you are about to see is part of the Player script): http://pastebin.com/2NRKyyaH
And now for the Sound Menu script, which is where I want the sound to either turn off or on without affecting the music: http://pastebin.com/5agXeq65 As for the music menu, it is almost exactly the same as the sound, so I won't be posting it.
Sorry for all these edits. I just want to make sure everyone knows how my scripts work(though it's understandable if you still don't). Please help if you have time. Thanks.
You can set two Global boolean variables, one for music and one for sfx then have the audio play script check whichever boolean is appropriate before playing the sound.
So something like this?:
public bool music;
public bool sound;
And here's where I put my audio for my jump sounds:
public AudioClip[] JumpAudio;
AudioSource audio;
if (grounded && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
anim.SetBool ("Ground", false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
AudioSource.PlayClipAtPoint(JumpAudio[ Random.Range(0, JumpAudio.Length) ], transform.position, .2f);
}
}
And my music(I have two tracks so far):
public AudioClip[] songs;
int currentSong = 0;
if (audio.isPlaying == false){
currentSong++;
if(currentSong >= songs.Length)
currentSong = 0;
audio.clip = songs[ currentSong ];
audio.Play();
}
}
public void SpeedUp()
{
audio.Stop (); //pauses the slow song
audio.clip = songs [0]; //gets the fast song
}
void End()
{
Debug.Log ("ending");
audio.Stop (); //stops the fast song
//audio.clip = songs [1]; //plays the slow song
}
Answer by smallbit · Jul 17, 2014 at 01:39 AM
what he meant probably was something like this
if (GUI.Button (new Rect (Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .1f), " Audio On"))
{
sound = true; // set your flag
}
if (GUI.Button (new Rect (Screen.width * guiPlacementX3, Screen.height * guiPlacementY3, Screen.width * .5f, Screen.height * .1f), "Audio Off"))
{
sound = false; // set your flag
}
//make the same pair for background music
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(music)//use flag
AudioSource.PlayClipAtPoint(JumpAudio[Random.Range(0, JumpAudio.Length)], transform.position, .2f);
}
From my experience the best practice is to have a proper method (i.e. void PlaySound(Audioclip clip, Transform position)) to play sound in which you check the flag and pass clip as parameter. However depending on the setup would be easier to have separated audio sources for sound effects and music than just use audioSource play instead of playclipatpoint, than again depends on the setup.
Hello, thanks for trying to explain this to me. I'm still new to Audio so some of these things are confusing. Anyways what would be the best setup for playing sounds when the character jumps, sounds play when click buttons in the menu, and music playing on the main menu, the game, and when I collect a power up? Is my setup okay? Or in other words, can I work with the setup I currently have? I'm sure you already answered these questions. I'm just trying to sort things out in my head.
Alright so I changed my setup a little bit. Here's how it works: I have the sounds of the jump on my Player Script, and the background music on a different gameobject on a different script. The jump sounds are the same as the script I posted, and the music audio is on a different script, here's the script:
public class $$anonymous$$usicTest : $$anonymous$$onoBehaviour {
AudioSource audio;
public AudioClip[] songs;
int currentSong = 0;
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
if (audio.isPlaying == false){
currentSong++;
if(currentSong >= songs.Length)
currentSong = 0;
audio.clip = songs[ currentSong ];
audio.Play();
}
}
}
Now how can I disable both the sounds and the music individually without disabling them all together?
Your answer
Follow this Question
Related Questions
Audio Scripting 1 Answer
Audio Play Once 2 Answers
Trigger audio loop on beat with PlayScheduled 1 Answer
Play from a variety of sounds? 1 Answer
Audio: how do I immediately play a new audioclip without delay(code included)? 2 Answers