Search Unity

How To play audio on button press unity 5?

Discussion in 'Scripting' started by sunseeker1988, Jul 11, 2015.

  1. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    395
    I cannot figure out how to get a button to play audio clip when pressed. Here is the following steps someone wrote:
    1. Add an audio source GameObject to your scene
    2. Assign an audio clip to it
    3. Uncheck "Play on Awake"
    4. Go to the OnClick() section of the UI Button
    5. Click the plus sign to add an item to the list
    6. Assign the Audio Source object to the object field
    7. 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.
     
    Last edited: Jul 11, 2015
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,450
    Why not just make your own script. AudioUnit. It can have a clickPlay() function that will be seen.
     
  3. Rad-Coders

    Rad-Coders

    Joined:
    Apr 10, 2015
    Posts:
    36
    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.

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AudioPlay : MonoBehaviour {
    5.  
    6.     public AudioSource sound;
    7.     private Rect audiorect;
    8.  
    9.     void Start ()
    10.     {
    11.         audiorect = new Rect (20, 20, 100, 20);
    12.     }
    13.    
    14.  
    15.     void Update ()
    16.     {
    17.  
    18.     }
    19.  
    20.     void OnGUI()
    21.     {
    22.             GUI.Label(audiorect, "Play Audio");
    23.  
    24.             if(audiorect.Contains(Event.current.mousePosition))
    25.             {
    26.                 if(Input.GetMouseButtonDown(0))
    27.                 {
    28.                     sound.Play();
    29.                 }
    30.                
    31.  
    32.                 }
    33.         }
    34.     }
    35.  
    36.  
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,462
    Create your own script that has a public function that plays the AudioSource and link that function to your UI Button.
     
    sunseeker1988 likes this.
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,450
    Code (csharp):
    1.  
    2. public AudioSource audioSource;
    3. public AudioClip audioClip;
    4.  
    5. public void playClip(){
    6. audioSource.clip = audioClip;
    7. audioSource.Play();
    8. }
     
  6. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    395
    Worked perfect Thanks every One!
     
    renman3000 likes this.
  7. mafanbong8819

    mafanbong8819

    Joined:
    Feb 2, 2017
    Posts:
    8
    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
     
    Renancepar, Fbryant77, Rallix and 3 others like this.
  8. nubick

    nubick

    Joined:
    Nov 17, 2012
    Posts:
    29
    darknubis365 and sunseeker1988 like this.
  9. wmaass88

    wmaass88

    Joined:
    Dec 23, 2012
    Posts:
    43
    Thanks nubick, that saved me a lot of work!
     
  10. Hoorza

    Hoorza

    Joined:
    May 8, 2016
    Posts:
    20
    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.
     
    sunseeker1988 and mafanbong8819 like this.
  11. ben-rasooli

    ben-rasooli

    Joined:
    May 1, 2014
    Posts:
    25
    Simply attach this script to your button and provide an audio clip. (This is a PLUG & PLAY component)
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ButtonTapSound : MonoBehaviour
    5. {
    6.     [SerializeField] AudioClip _audioClip;
    7.     [SerializeField] [Range(0.0f, 1.0f)] float _volume = 1;
    8.     AudioSource _audioSource;
    9.  
    10.     void Awake ()
    11.     {
    12.         _audioSource = gameObject.AddComponent<AudioSource> ();
    13.         if (_audioClip != null)
    14.             _audioSource.clip = _audioClip;
    15.         _audioSource.playOnAwake = false;
    16.         _audioSource.volume = _volume;
    17.         GetComponent<Button> ().onClick.AddListener (() => _audioSource.Play ());
    18.     }
    19. }
     
    Last edited: May 10, 2017
    sunseeker1988 likes this.
  12. sweebach

    sweebach

    Joined:
    Feb 3, 2014
    Posts:
    1
    sunseeker1988 and nubick like this.
  13. LilGames

    LilGames

    Joined:
    Mar 30, 2015
    Posts:
    197
    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.
     
  14. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    150
    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.
     
    sunseeker1988 likes this.
  15. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    86
    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!.
     
  16. darknubis365

    darknubis365

    Joined:
    Nov 12, 2017
    Posts:
    12
    Agreed this worked for me as well thank u!
     
  17. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    when i am audio set rate button open the url and then play a audio??
     
  18. fichan

    fichan

    Joined:
    Aug 14, 2019
    Posts:
    1
    how to make audio button with once playing audio