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 polda602 · Aug 09, 2014 at 08:34 PM · guibuttonsound

GUI Button sound problem, don´t work

Hi i´m trying make a pause menu with gui and i want when the mouse will be on button play sound, i already have sound when clicking but not when i hover mouse on it there is my code: thanks for help

var windowPause:Rect=Rect(1000,1000,1000,1000); private var showPause:boolean;

 var enter : AudioClip;
 var press : AudioClip;
 
 var buttonRect : Rect = Rect(110,120,70,30);
 
 function Update () {
 if (Input.GetButtonUp ("Pause")) {
        if (showPause == true) {
          showPause = false;
          Time.timeScale = 1;
         }
         else if (showPause == false){
         showPause = true;
         Time.timeScale = 0;
         }
      }
 
      
 }
 
  function OnGUI () {
  if (buttonRect.Contains(Input.mousePosition)) {
  audio.PlayOneShot (enter);
  }     
           
              if (showPause==true){
               windowPause= GUI.Window(1, windowPause, DoMyWindowPause, "Pause");
               
                 
            }
     }
     
 function DoMyWindowPause () {
 if (GUI.Button(Rect(110,80,70,30),"Resume")){
 audio.PlayOneShot(press);
 showPause = false;
 Time.timeScale = 1;
 }
 if (GUI.Button(buttonRect,"Setting")){
 audio.PlayOneShot(press);
 }
 if (GUI.Button(Rect(110,160,70,30),"Exit")){
 audio.PlayOneShot(press);
 Application.LoadLevel(0);
 Time.timeScale = 1;
 }
 }
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 robertbu · Aug 10, 2014 at 04:44 AM

You have three problems here. First you are using Screen coordinates in a Rect.Contains() with a rect defined in GUI coordinates. Second, you call 'PlayOneShot()' repeatedly (every frame when the mouse is in the rect). Third, when you click the button, you are also going to evaluate to true for the 'buttonRect.Contains(). Here is a bit of untested code. You might have to go with a latching mechanism where you don't play 'enter' a second time rather than using Input.GetMouseButtonDown(0):

 function OnGUI () {
      if (!audio.isPlaying && buttonRect.Contains(Event.current.mousePosition) && !Input.GetMouseButtonDown(0)) {
          audio.PlayOneShot (enter);
      }     
  
      if (showPause==true){
               windowPause= GUI.Window(1, windowPause, DoMyWindowPause, "Pause");
  
      }
 }
Comment
Add comment · Show 1 · 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 polda602 · Aug 10, 2014 at 12:18 PM 0
Share

thanks for answer and helping, but still it isn´t work correctly, when i playing the game and with mouse move the on certain position it´s in at top right corner somewehere there, and when i reach this position the sound is playing constantly, don´t understand but thanks for help !

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

2 People are following this question.

avatar image avatar image

Related Questions

Button and resolution problem 1 Answer

Scale gui button to resolution and playing audio 1 Answer

same background image and buttons to work with different apple devices 1 Answer

Tooltip does not show 1 Answer

Play sound on button click before loading level. 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