How To play audio on button press unity 5?

I cannot figure out how to get a button to play audio clip when pressed. Here is the following steps someone wrote:

  • Add an audio source GameObject to your scene
  • Assign an audio clip to it
  • Uncheck “Play on Awake”
  • Go to the OnClick() section of the UI Button
  • Click the plus sign to add an item to the list
  • Assign the Audio Source object to the object field
  • Choose AudioSource.Play in the dropdown

There is no AudioSource.Play in the drop down only the words No Function…

Anyone have any idea how to get this working? You will be helping me greatly. I have browsed countless forums and youtube videos. Is there still a way to play audio file on button click without a script?

I have 144 audio clips that will be assigned to 36 buttons on screen 2, depending upon which item on screen 1 is selected it will assign that items sound to one of screen 2 36 buttons. the 36 buttons are visible all the time on screen 2. do i need to creat 144 audio sources each with there own audio clip attached and assign that to a button? or is there a more efficient method. Im very new to scripting just to let every one know.

5 Likes

Why not just make your own script. AudioUnit. It can have a clickPlay() function that will be seen.

2 Likes

Here is how I would do it. It may have a few bugs but you should just understand the concept. I don’t like the built in button functions but you can use it if you want to. Just create a Audio source, un tick play at start and select the audio clip. And drag the audiosource onto the script. Here is the script, let me know if you need help.

 using UnityEngine;
using System.Collections;
 
public class AudioPlay : MonoBehaviour {
 
    public AudioSource sound;
    private Rect audiorect;
 
    void Start ()
    {
        audiorect = new Rect (20, 20, 100, 20);
    }
   
 
    void Update ()
    {
 
    }
 
    void OnGUI()
    {
            GUI.Label(audiorect, "Play Audio");
 
            if(audiorect.Contains(Event.current.mousePosition))
            {
                if(Input.GetMouseButtonDown(0))
                {
                    sound.Play();
                }
               
 
                }
        }
    }
3 Likes

Create your own script that has a public function that plays the AudioSource and link that function to your UI Button.

1 Like
public AudioSource audioSource;
public AudioClip audioClip;

public void playClip(){
audioSource.clip = audioClip;
audioSource.Play();
}
6 Likes

Worked perfect Thanks every One!

1 Like

button to sound no need script also can do.

here got an example,

  1. create button
  2. input sound
  3. add audio source
  4. uncheck play awake
  5. audio source add sound
  6. over the inspector of button, add list to the onclick function
  7. then choose audio source as object
  8. over function there choose audiosource
  9. then drag ur sound to the audio
  10. done
9 Likes

You can use following Unity editor extenstion for buttons click sound management: GitHub - nubick/unity-button-sounds-editor: Editor extension for Unity game engine. It helps to assign AudioClip to buttons and to play sounds by button clicks.

2 Likes

Thanks nubick, that saved me a lot of work!

3 Likes

You Tube clip that “mafanbong8819” posted is awesome for this. 2:30 mins tutorial and you attach sound without a script to the button. Really good way. If some one knows any problems with it pleasze share.

2 Likes

Simply attach this script to your button and provide an audio clip. (This is a PLUG & PLAY component)

using UnityEngine;
using UnityEngine.UI;

public class ButtonTapSound : MonoBehaviour
{
    [SerializeField] AudioClip _audioClip;
    [SerializeField] [Range(0.0f, 1.0f)] float _volume = 1;
    AudioSource _audioSource;

    void Awake ()
    {
        _audioSource = gameObject.AddComponent<AudioSource> ();
        if (_audioClip != null)
            _audioSource.clip = _audioClip;
        _audioSource.playOnAwake = false;
        _audioSource.volume = _volume;
        GetComponent<Button> ().onClick.AddListener (() => _audioSource.Play ());
    }
}
1 Like

Nubick is my hero today!

2 Likes

Just wanted to point out to everyone that the solutions above are problematic if you intend to set your button as inactive after clicking (ie: you want it to go away and be invisible). The sound will get cut off as soon as the button gameobject is deactivated.

1 Like

Simply add to the button an “Event Trigger”, Pointer Enter, add the sound from hierarchy, and AudioSource.Play. Works perfectly here and no script needed.

1 Like

Thank You So Much…Its the only way i got my button worked!..its been 12-13 hours since i’m trying to fix this thing and sometimes the audio plays,sometime it doesn’t and whole lot of mess but this method worked…Thanks Again!.

Agreed this worked for me as well thank u!

when i am audio set rate button open the url and then play a audio??

how to make audio button with once playing audio

this works for me too

public class ButtonSound : MonoBehaviour
{
    public AudioSource clickSound;

    // Start is called before the first frame update
    void Start()
    {

    }

    void Update()
    {

    }

    public void onClick()
    {
        clickSound.Play();
    }

:3