Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Meriadoc · Oct 23, 2014 at 07:58 PM · javascriptsoundhoverguibuttonguicontent

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();
 }
 

 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 }

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meriadoc · Oct 23, 2014 at 08:41 PM 0
Share

Is this what you mean by placing the hover on top of the function

 function OnGUI () { hover = GUI.tooltip;
 



avatar image Meriadoc · Oct 23, 2014 at 08:42 PM 0
Share

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

avatar image bubzy · Oct 23, 2014 at 08:43 PM 0
Share

yup, that's it

you can put it on the line underneath though, it looks nicer :)

avatar image Meriadoc · Oct 23, 2014 at 08:55 PM 0
Share

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?

avatar image bubzy · Oct 23, 2014 at 08:58 PM 0
Share

I didn't realise you are using javascript, did you declare the Boolean correctly?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges