Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dogg · Jul 17, 2014 at 12:59 AM · c#audiosoundmusic

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awest · Jul 17, 2014 at 01:03 AM 0
Share

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.

avatar image Dogg · Jul 17, 2014 at 01:25 AM 0
Share

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
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dogg · Jul 17, 2014 at 02:12 AM 0
Share

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.

avatar image Dogg · Jul 24, 2014 at 10:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges