- Home /
play sound on click
I am trying to play a sound when a button is clicked but it doesn't work. I have tried both public AudioClip Miss; and public UnityEngine.AudioClip Miss; and latter on I say audio.PlayOneShot(Miss);
I also Selected the audio file and yet when I click in game it says "MissingComponentException There is no AudioSource attached to the GLADIUS game object, but a script is trying to access it. You probably need to add a AudioSource to the game object GLADIUS. Or your script needs to check if the component is attached before using it."
Answer by liszto · Dec 19, 2012 at 12:07 AM
AS Unity says, you don't have Audiosource in your scene. Without an audiosource, your game cannot read/play a sound peacefully.
To read your audioclip, audio.PlayOneShot( yourAudioclip) can work but you need to do this on your object at start before :
public AudioClip myclip;
// Use this for initialization
void Start ()
{
this.gameObject.AddComponent<AudioSource>();
this.GetComponent<AudioSource>().clip = myclip;
this.GetComponent<AudioSource>().Play();
}
I invite you to read this documentation about AudioSource and AudioClip
P.S : You need to have an AudioListener in your scene (your camera have one per default)
Answer by nubick · Apr 02, 2017 at 08:55 AM
You can use following Unity editor extenstion for buttons click sound management: https://github.com/nubick/unity-button-sounds-editor
Dude thank you so much you have just saved me hours of work with this tool! Thank you sooo much!
Please read tutorial here: http://nubick.ru/button-sounds-editor-for-unity/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
PlayClipAtPoint Qualify with Type Name 2 Answers
OnDestroy() : Some objects were not cleaned up when closing the scene 1 Answer
Playing audio 1 Answer
Help with an error urgent 2 Answers