- Home /
Sound gets strectched when I hover over a button
I am working on a main menu where if you hover over a button it will play a sound, for example if I hover over the start button, a sound will play: "start the game" but the problem is when I hover over the sound it gets stretched.
This is my code
//Script Main Menu
//Inspector Var's
var hover : String;
var StartGame : AudioClip;
var Credits : AudioClip;
var Exit: AudioClip;
var HomePage: AudioClip;
var Tutorial: AudioClip;
//Private Var's
function OnGUI ()
{
//make a group on the centre of the screen
GUI.BeginGroup (Rect(Screen.width / 2-50, Screen.height /2-50,100,205));
//make a box to see the group on screen
GUI.Box (Rect(0,0,100,205),"Main Menu");
//add buttons for game navigation
if (GUI.Button (Rect(10,30,80,30), GUIContent ("Start Game", "Button 1")))
{
Application.LoadLevel("screenLoad");
}
hover = GUI.tooltip;
if(hover=="Button 1")
audio.PlayOneShot(StartGame);
//add buttons for game navigation
if (GUI.Button (Rect(10,65,80,30), GUIContent ("Credits", "Button 2")))
{
Application.LoadLevel("screenCredit");
}
hover = GUI.tooltip;
if(hover=="Button 2")
audio.PlayOneShot(Credits);
//add buttons for game navigation
if (GUI.Button (Rect(10,100,80,30), GUIContent ("Exit", "Button 3")))
{
Application.Quit();
}
hover = GUI.tooltip;
if(hover=="Button 3")
audio.PlayOneShot(Exit);
//add buttons for game navigation
if (GUI.Button (Rect(10,135,80,30), GUIContent ("Homepage", "Button 4")))
{
Application.OpenURL("http://meria-doc.tumblr.com");
}
hover = GUI.tooltip;
if(hover=="Button 4")
audio.PlayOneShot(HomePage);
if (GUI.Button (Rect(10,169,80,30), GUIContent ("Tutorial", "Button 5")))
{
Application.LoadLevel("TutorialLoad");
}
hover = GUI.tooltip;
if(hover=="Button 5")
audio.PlayOneShot(Tutorial);
GUI.EndGroup();
}
Answer by bubzy · Oct 23, 2014 at 08:04 PM
the sound is continually playing.
try a Boolean like
bool soundPlayed = false;
then in your other function
if(hover=="Button 3" && !soundPlayed)
{
soundPlayed = true;
audio.PlayOneShot(Exit);
}
you can then unset it when you are not hovering over anything
something like
if (hover == "") //or whatever it returns when you are not over a button.
{
soundPlayed = false;
}
edit : you don't need to keep getting the value for hover
just once will do, put it at the top of the function.
edit 2 :
also a switch would be nice there
switch(hover)
{
case : "Button 1"
{
if(!soundPlayed)
{
soundPlayed = true;
playSound(StartGame);
}
break;
}
case : "Button 2"
{
if(!soundPlayed)
{
soundPlayed = true;
playSound(Credits);
}
break;
}
case : "Button 3"
{
if(!soundPlayed)
{
soundPlayed = true;
playSound(Exit);
}
break;
}
}
void playSound(AudioClip clip)
{
audio.PlayOneShot(clip);
}
Is this what you mean by placing the hover on top of the function
function OnGUI () { hover = GUI.tooltip;
and does this look right?
if (GUI.Button (Rect(10,30,80,30), GUIContent ("Start Game", "Button 1")))
{
Application.LoadLevel("screenLoad");
}
if(hover=="Button 1" && !soundPlayed)
{
soundPlayed = true;
audio.PlayOneShot(StartGame);
}
if (hover == "") //or whatever it returns when you are not over a button.
{
soundPlayed = false;
}
NOTE: I am still new to Unity so sorry if I don't understand
yup, that's it
you can put it on the line underneath though, it looks nicer :)
I put the hover at the top and deleted the other ones and now i get no sound. Also I don't understand how to apply the switch where should i put it?
I didn't realise you are using javascript, did you declare the Boolean correctly?
Your answer
![](https://koobas.hobune.stream/wayback/20220613165806im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Play sound when out of ammo 0 Answers
GUIButton (outline) constant glowing/flashing? 1 Answer
I have problems with coding 0 Answers
Play sound when clicking object 1 Answer
change detail in game an the sound? 3 Answers