Playing Sound when mouse touches text

Hi,

I’m new to Unity and new to JavaScript. But I have an error when I use this code.
It supposed to be playing a little sound when you touch the text with your mouse.
But it gives the following error:

Everything else in the script works perfectly except for the line with audio.Play = sound;
Don’t kill me if its just a simple and noobish mistake.

Here is the script, it is based on an script I found online.
I’ve added it as an attachment for if you wish to use it or edit it.

var isQuit=false;
var sound : AudioClip;

function OnMouseEnter(){
    //change text color
    renderer.material.color=Color.red;
    // Play sound
    audio.Play = sound;
}

function OnMouseExit(){
    //change text color
    renderer.material.color=Color.white;
}

function OnMouseUp(){
    //is this quit
    if (isQuit==true) {
        //quit the game
        Application.Quit();
    }
    else {
        //load level
        Application.LoadLevel(1);
    }
}

function Update(){
    //quit game if escape key is pressed
    if (Input.GetKey(KeyCode.Escape)) {
            Application.Quit();
    }
}

Thanks anyway!

1728237–109015–gameMenu.js (532 Bytes)

To play custom AudioClip, use audio.PlayClipAtPoint(sound,transform.position,1f); instead audio.play();

Play is a function of AudioSource and can’t be assigned something like that. You either have to:

  1. Set the clip to be equal to your sound, then call audio.Play()
  2. Use another method (like @IsGreen suggested) such as PlayOneShot()

You can find a full list of what the AudioSource class can do here.

Thanks so mutch :smile: