- Home /
When mouse on button needs to play sound
Hello guys.
I got:
1)Gameobject - _Music2
2)JS file - Music2
3)Music file - music2
My JS file code:
var sound : AudioClip ;
function MouseOnEnter () {
audio.PlayOneShot(music2);
}
I add my JS script to my gameobject (_Music2) And i add my music file to my gameobject. 
I add gameobject (_Music2) to my button. Because i want when mouse go on button, it make sound.. 
I add my music file (music2) to my JS file (Music2) I start my scene, but sound doesnt play.. why?
Have you tried either: using an audio source, or using a GUI.Button ins$$anonymous$$d?
I have try to use audio source, i have add audio source to my gameobject. And in on click() i have press Audio source Play (). I will try with GUI.Buttons.
Sorry, got fail... Gui button isnt ui button... i dont like gui buttons, dont wanna try with that.. i like ui buttons..
Answer by Thom Denick · Jan 02, 2015 at 12:51 AM
If there are no errors, this is likely because your audioclip is not set to 2D, and the clip is playing too far from your AudioListener.
$$anonymous$$y audioclip is set to 2D. What i need to do, to get clip closer to AudioListener? I can try to drag my gameobject closer to my main camera, where is audio listener. Or i need drag my button, what i want to make sound, to closer to main camera?
Have try my gameobject drag closer to my main camera where is audio listener, but sound doesnt play...
$$anonymous$$yb someone can write step by step what need to do? Than i will check if i have all steps do..
If it's set to 2D, I was wrong. Your code is fine, so it must be something else wrong. Does the clip preview fine in Unity Editor? What happens when you put on an AudioSource attached to the same GameObject as the AudioListener with "AutoPlay" checked on?
Your answer