Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by OC1 · Mar 21, 2016 at 02:23 PM · uiaudiosoundmouse position

Play sound (OnMouseOver)

When the cursor is over the object i want play a sound one time

  • A lot of code in the community is obsolete

What i can do?

        using UnityEngine;
        using System.Collections;
        using UnityEngine.SceneManagement;
        using UnityEngine.UI;

         public class MMM : MonoBehaviour {
 /*Sound*/
 public AudioClip MySound;
 /*MENU*/
 public void New(){
     Debug.Log ("New");
     SceneManager.LoadScene("MapTry");
 }
 public void Continue(){
     Debug.Log ("Continue");
 }
 public void Options(){
     Debug.Log ("Options");
 }
 public void Quit(){
     Debug.Log ("Quit");
     Application.Quit();
 }
 /*OnMouseOver*/
 void OnMouseOver(){
     Debug.Log ("Play_Audio");
     GetComponent<AudioSource>().Play();
 }
 }
Comment
Add comment · Show 2
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 ZefanS · Mar 23, 2016 at 07:54 AM 0
Share

By play one time do you mean on the first mouse-over and then not on any subsequent mouse-overs? Or do you just mean play the sound each time without repeating?

avatar image OC1 ZefanS · Mar 23, 2016 at 06:02 PM 0
Share

I just want play a sound one time, when the mouse is passing over the button.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ZefanS · Mar 24, 2016 at 01:37 AM

Instead of using Play(), you should use PlayOneShot(). I'd recommend the following changes: First, add this code at the top:

 public AudioSource audioSource;
 
 void Start()
 {
     audioSource = GetComponent<AudioSource>();
 }

This will save a reference to the Audio Source because you should try and avoid using GetComponent() repreatedly.

In the inspector for the Audio Source uncheck Play On Awake and Loop. You can also leave the AudioClip slot unassigned.

Also note that OnMouseOver() is "called every frame while the mouse is over the GUIElement or Collider." In other words you'll want to switch that to OnMouseEnter(), which will only be triggered on the first frame that the cursor is over the button.

Finally, to actually play the sound, inside OnMouseEnter() add the line:

 audioSource.PlayOneShot(mySound);

The final script should look something like this:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class MMM : MonoBehaviour
 {
     /*Sound*/
     public AudioClip mySound;
     public AudioSource audioSource;
 
     void Start()
     {
         audioSource = GetComponent<AudioSource>();
     }
 
     /*MENU*/
     public void New()
     {
         Debug.Log ("New");
         SceneManager.LoadScene("MapTry");
     }
     public void Continue()
     {
         Debug.Log ("Continue");
     }
     public void Options()
     {
         Debug.Log ("Options");
     }
     public void Quit()
     {
         Debug.Log ("Quit");
         Application.Quit();
     }
 
     /*OnMouseEnter*/
     void OnMouseEnter()
     {
         Debug.Log ("Play_Audio");
         audioSource.PlayOneShot(mySound);
     }
 }

Hope this helps!

Comment
Add comment · Show 2 · 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 OC1 · Mar 24, 2016 at 02:21 AM 0
Share

I need to do something else like add EmptyObject with AudioSourse?

avatar image ZefanS OC1 · Mar 24, 2016 at 02:23 AM 0
Share

Add an Audio Source to the GameObject that has this script attached to it (the menu I'm assu$$anonymous$$g).

avatar image
0

Answer by OC1 · Mar 24, 2016 at 02:48 AM

I'm passing the cursor to the button, but don't play the sound, but if i click on it sounds

Why?

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;

 public class MMM : MonoBehaviour {
 
 /*Sound*/
 public AudioClip Mysound;
 public AudioSource MyAudioSource;

 /*MENU*/
 void Start(){
     MyAudioSource = GetComponent<AudioSource>();
 }

 public void New(){
     Debug.Log ("New");
     SceneManager.LoadScene("Intro00");
 }
 public void Continue(){
     Debug.Log ("Continue");
 }
 public void Options(){
     Debug.Log ("Options");
 }
 public void Quit(){
     Debug.Log ("Quit");
     Application.Quit();
 }
 /*OnMouseOver*/
 public void OnMouseEnter(){
     Debug.Log ("Play_Audio");
     MyAudioSource.PlayOneShot(MySound);
 }
 }
Comment
Add comment · Show 7 · 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 ZefanS · Mar 24, 2016 at 02:52 AM 1
Share

Does the debug statement print when you mouse over the buttons?

Also, don't post follow up questions as answers, post them as comments.

avatar image OC1 ZefanS · Mar 24, 2016 at 03:04 AM 0
Share

Nope juts when i click on it i have this in the inspector Button:

OnClick() $$anonymous$$yFunction

But dont sounds

avatar image ZefanS OC1 · Mar 24, 2016 at 03:11 AM 1
Share

Have you taken a look a the documentation for On$$anonymous$$ouseEnter()? The script containing the call to On$$anonymous$$ouseEnter() needs to be attached to the object with the GUIElement / Collider that you're detecting the mouse over on. You have to set up your menu and components correctly or it won't work.

Show more comments
avatar image OC1 ZefanS · Mar 25, 2016 at 12:13 AM 0
Share

Thank you now is working!! (I miss the Event Trigger).

avatar image ZefanS OC1 · Mar 25, 2016 at 12:54 AM 1
Share

Glad to 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# On Click sound to play before loading scene 0 Answers

Use Android default sound effect on a button 0 Answers

Cracking at end of audio? 0 Answers

[Need help] How to higlight button color when passing the mouse after a click? 1 Answer

How to handle simultaneous sound with Unity 5 Audio 1 Answer


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