Search Unity

Audio How to Toggle audio clip on / off?

Discussion in 'Audio & Video' started by conan222, Feb 7, 2019.

  1. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Hi,

    I'm trying to create a toggle button that sits on a panel and will turn some audio on and off.

    Panel loads, there's pictures and some text on the panel and also a toggle switch. When I click the switch the sound clip plays and when I click it again the sound clip stops.

    An example would be a picture of a dog on the panel and when the toggle button is clicked you can hear a barking sound. Click again and it turns off.

    I've tried many tutorials but can't seem to achieve what I need as many just mute the sound or even worse mute all the sounds in the scene :confused:

    Any help is greatly appreciated and if there's a good reasonably priced asset for sale that will accomplish this I don't mind buying it.


    Thanks,

    Andrew
     
    Last edited: Feb 7, 2019
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    this is pretty simple, create a button (create, ui, button) and make the button run a script when it is clicked. You edit the button where it says OnClick and tell it which script to run.

    In that script you need to have some code that find the AudioSource and changes the volume. If you look at my previous comment on another thread you will see some code I posted.

    I will find it and link it here.
    https://forum.unity.com/threads/getting-a-int-float-for-the-volume-of-the-sound.632683/#post-4255312