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.
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();
}
}
}
}
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.
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.
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.
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!.
public class ButtonSound : MonoBehaviour
{
public AudioSource clickSound;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
}
public void onClick()
{
clickSound.Play();
}