I have a script put together to change the color of text on a mouse-over. I also want to play sound should the person click on this text.
Existing Script:
//Begin
var soundfx : AudioClip;
function OnMouseEnter()
{
//Change the Color of the Selection
renderer.material.color = Color.red;
if (Input.GetButton(“Fire1”))
{
audio.PlayOneShot(soundfx);
}
}
// End
This looks to me like it would work, and it compiles successfully, but it does not play the sound when clicked on.
Advice?
Any errors when you run it? like “there is no audio source” ?
There are no errors. If I take the If statement out of OnMouseEnter() and put it in its own function, the sound plays anytime I click anywhere on the screen, not just on the 3D text. So I tried putting it inside the OnMouseEnter() function to somehow tie it to that, but I’m getting no results.
I think the main issue is the function OnMouseEnter().
OnMouseEnter() only calls the first frame your mouse enters the GUIElement or Collider it is attached to. Currently, if you hold the Fire1 button down (I changed mine to a input.GetMouseDown(0) ) before you enter the GUIElement, continue to hold the mouse, and then mouse over the gui element, it will play a sound.
The best way to do this may be to split up your script.
So, you can keep OnMouseEnter for the render color change and then have an OnMouseDown call for the sound:
function OnMouseEnter()
{
renderer.material.color = Color.red;
}
function OnMouseDown()
{
audio.PlayOneShot(soundfx);
}
@Keryu, I never thought of doing it that way, that worked perfectly!