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();
}
}
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?
I just want play a sound one time, when the mouse is passing over the button.
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!
I need to do something else like add EmptyObject with AudioSourse?
Add an Audio Source to the GameObject that has this script attached to it (the menu I'm assu$$anonymous$$g).
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);
}
}
Does the debug statement print when you mouse over the buttons?
Also, don't post follow up questions as answers, post them as comments.
Nope juts when i click on it i have this in the inspector Button:
OnClick() $$anonymous$$yFunction
But dont sounds
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.
Thank you now is working!! (I miss the Event Trigger).