Documentation

Support

Unity Studio

Open Unity Studio

Unity Studio

Mute and unmute audio with the toggle element.

Use a toggle to mute and unmute an audio source.
Read time 2 minutesLast updated 2 hours ago

Use this example to understand how to mute and unmute a scene's audio using a toggle.
1

Create a toggle in your UI Canvas

  1. Select UI Canvas in the Hierarchy panel.
  2. Right-click the UI Canvas and select UI > Toggle to create a new toggle.
  3. Expand the toggle object in the Hierarchy panel. The toggle's child objects show.
  4. Select the Label child object.
  5. In the Inspector window, in the Text field, type
    Mute
    . Your toggle's label updates.
2

Add audio to your scene

  1. Find an audio clip in your My Assets folder or select Add Assets to import one.
  2. Drag the audio into the scene. Unity Studio creates an Audio Source GameObject.
3

Create a Logic script

  1. In the Project panel, select the My Logic folder.
  2. Select Add (+).
  3. Select Script. Unity Studio creates a new Logic script in the folder.
  4. Enter a name for your script and press Enter.
4

Add the script to a GameObject

  1. Select a GameObject in the Hierarchy panel.
  2. From the Inspector panel, select Add Component.
  3. Choose your Logic script from the list.
5

Set up the initial logic in the Logic Editor

  1. In the Project panel, double-click the Logic script to open the Logic Editor.
  2. Drag a When event listener into the Logic Editor.
  3. Add a Play starts event inside the When node.
  4. Create the following variables:
  • Unique name :
    toggle
    , Type :
    Toggle
  • Unique name :
    background_music
    , Type :
    Audio Source
6

Create the toggle listener

  1. Drag the
    toggle
    variable into the script, expand it, and choose onValueChanged > AddListener to create a listener that detects user interaction.
  2. Add a Set variable value node inside the listener.
  3. Drag the
    background_music
    variable into the script, expand it, and choose mute to create a Boolean node, and place it in the first socket of the Set variable value node.
7

Bind toggle state to mute

  1. Duplicate the Set variable value node (right-click > Duplicate).
  2. Drag another
    toggle
    variable into the script, expand it, and choose isOn to create a toggle > isOn Boolean node.
  3. Place the isOn node in the second socket of the duplicated Set variable value node.
  4. Move the duplicated node into the When node to sync the mute state when the scene starts.
8

Connect the listener for interaction and finalize the setup

  1. Move the original Set variable value node (with empty second socket) into the toggle > onValueChanged > AddListener node.
  2. Click and drag the evtBoolNode from the listener (which creates a copy) into the node’s second socket so the mute updates on interaction.
  3. Drag this listener group into the When node.
Example Logic script to mute and unmute an audio source when you interact with a toggle.

Example Logic script to change the audio source's mute state on toggle.

  1. Close the Logic Editor.
9

Connect your Toggle and Audio Source references

  1. In the Hierarchy panel, select the GameObject with the attached Logic script.
  2. Drag your toggle from the Hierarchy panel into the toggle variable reference in your script component.
  3. Drag your Audio Source object from the Hierarchy panel into the background_music variable reference in your script component.

Additional resources