- Home /
Question by
Blayer98 · Jun 07, 2015 at 09:37 PM ·
audioaudiosourceaudiocliponmouse
How to get AudioSource to play a Sound correctly?
What I mean is play it when the event, e.g: OnMouseEnter, occurs. Here's my script.
using UnityEngine;
using System.Collections;
public class ButtonSFX: MonoBehaviour {
void OnMouseEnter()
{
GetComponent<AudioSource>().Play();
}
void OnMouseExit()
{
GetComponent<AudioSource>().Stop();
}
void OnMouseDown()
{
{
GetComponent<AudioSource>().Play();
Application.LoadLevel(1);
}
}
}
Also, is there anyway to play a DIFFERENT sound when OnMouseDown happens? Thanks!
Comment
Answer by ahmedbenlakhdhar · Jun 07, 2015 at 10:46 PM
The functions OnMouseEnter()
, OnMouseExit()
and OnMouseDown()
are only called if the mouse interacts with a GUIElement or a Collider.
To play another audio clip than the one affected to the audio source, try the following:
GetComponent().PlayOneShot(anotherAudioClip);