How to mute your game in Unity

Music on/Off is a feature available in almost all games but how to implement that in your game? In this tutorial, we will learn how to mute your game using a UI button in Unity.

I am using Unity 2021.1.13 for this tutorial.

Create a new UI button and add the mute symbol to it. If you don’t have any image for your UI, you can download it for free from Unity assets store.

Create an empty game object and name it “UI_script”. Then add a script called UI_Control to it.

Free Unity asset banner

Copy and paste the code below.

using UnityEngine;

public class UI_Control : MonoBehaviour
{

   public void mute()
   {
    AudioListener.volume=0;
    
   }
}

Select the UI button and click the + sign to add an Onclick event.

Drag and drop the UI script gameobject the instance and select the function mute.

Now when you press the button all sounds in the game will be muted. To unmute the game, you can set

AudioListener.Volume=1

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.