Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 10, 2017 at 04:28 PM by mcarvalho4467 for the following reason:

The question is answered, right answer was accepted

avatar image
4
Question by mcarvalho4467 · Apr 03, 2017 at 04:45 PM · c#unity 5audio sourcemute

how can i mute all audio sources in the scene?

I already have a script attached to all the components that has an audio source, like the background music, the player's sounds and the enemy, who is a prefab. all sounds get mute when i press M, except for the enemy's sounds (hurt and death sounds). that's my script (c#):

using UnityEngine; using System.Collections;

public class Audio : MonoBehaviour {

 AudioSource audio;

 void Start() {
     audio = GetComponent<AudioSource>();
 }

 void Update() {
     if (Input.GetKeyDown(KeyCode.M))
     if (audio.mute)
         audio.mute = false;
     else
         audio.mute = true;

 }

}

how can i mute the enemy sounds as well?

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
14
Best Answer

Answer by sleepandpancakes · Apr 04, 2017 at 05:05 AM

Why not just mute the audio listener? That way all sounds are muted at once.

     public void ToggleSound()
     {
         toggle = !toggle;
 
         if (toggle)
             AudioListener.volume = 1f;
 
         else
             AudioListener.volume = 0f;
     }


Comment
Add comment · Show 1 · 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 mcarvalho4467 · Apr 04, 2017 at 12:18 PM 0
Share

omg i totally forgot about this! thanksssss

avatar image
0

Answer by Hellium · Apr 03, 2017 at 04:47 PM

I would use one "Mute manager" instead of multiple attached to each objects.

FindObjectsOfType is not resources efficient, and maybe, you can "cache it" instead of retrieving them each time. It depends on your project.

 public class MuteManager : MonoBehaviour {
 
     private bool muted ;
  
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.M))
             ToggleAudio();
     }
     
     public void DisableAudio()
     {
        SetAudioMute( false ) ;
     }
     
     public void EnableAudio()
     {
        SetAudioMute( true ) ;
     }
     
     public void ToggleAudio()
     {
         if( muted )
             DisableAudio();
         else
             EnableAudio();
     }
     
     private void SetAudioMute( bool mute )
     {
         AudioSource[] sources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
         for( int index = 0 ; index < sources.Length ; ++index )
         {
             sources[index].mute = mute ;
         }
         muted = mute ;
     }
 }
Comment
Add comment · Show 4 · 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 mcarvalho4467 · Apr 03, 2017 at 05:29 PM 0
Share

the script is not working, the following error keeps appearing: Assets/$$anonymous$$ute$$anonymous$$anager.cs(37,12): error CS1061: Type UnityEngine.AudioSource[]' does not contain a definition for mute' and no extension method mute' of type UnityEngine.AudioSource[]' could be found. Are you missing an assembly reference?

avatar image JoshDangIt mcarvalho4467 · Apr 03, 2017 at 05:35 PM 0
Share

He made a typo, try replacing line 34 with

 sources[index].mute = mute ;
avatar image mcarvalho4467 JoshDangIt · Apr 03, 2017 at 05:41 PM 0
Share

I fixed it and the error was gone, but it only mute the background music, what should i do?

Show more comments

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Object reference is NULL when IT IS set to an instance of an object? 1 Answer

Most efficient way to remove a null object from a dictionary 3 Answers

Running a script without being attached to an object? 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