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

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