- Home /
Duplicate Question
How do I play a sound after the mouse goes through an object?
I'm doing a menu for my game and I don't know how to play a sound after the mouse pass through it... I just made it change the color and load the next level. for example.
using UnityEngine;
using System.Collections;
public class play : MonoBehaviour {
public bool sair = false;
void OnMouseEnter ()
{
renderer.material.color = Color.blue;
}
void OnMouseExit ()
{
renderer.material.color = Color.white;
}
void OnMouseDown ()
{
if (sair)
{
Application.Quit();
}
else
{
Application.LoadLevel(1);
}
}
}
Answer by aaronov · May 09, 2013 at 11:57 PM
You need to have an AudioSource attached to your GameObject with an AudioClip assigned to it. Then you simply call Play() on the audio source within the OnMouseEnter() event. You will also need an AudioListener most likely attached to your main camera.
Answer by aldonaletto · May 10, 2013 at 12:30 AM
You can simply use audio.Play() to start playing the sound. Add an AudioSource to the menu object and set its Clip field to the desired sound, then add the instruction audio.Play() to the OnMouseEnter event - like this:
void OnMouseEnter ()
{
renderer.material.color = Color.blue;
audio.Play();
}
HINT: It's better to uncheck the 3D sound property in the clip (click the audio clip in the Project view to access its properties)
Answer by MalukoAlegre · May 10, 2013 at 01:20 AM
hehe...thanks...it worked..! I also put an song to play when the scene is loaded to stay as background music... =D
Follow this Question
Related Questions
How to Have a Random Interval in an Array in between Sounds being Played? 1 Answer
Make footstep script 2 Answers
Can't get my sound to work.. 1 Answer
How to play music and sound / get working radio 0 Answers
make sound work 0 Answers