x


Sound to play when clicking buttons on GUI

On the main menu of my scene I have created; attempting to link sound to gui buttons to play when clicked on...

Any idea how I would go about this?

This is the following code I have for my main menu scene:

var beep : AudioClip;
var menuSkin : GUISkin;
var areaWidth : float;
var areaHeight : float;

function OnGUI (){
    GUI.skin = menuSkin;
    var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
    var ScreenY = ((Screen.height * 0.5) - (areaHeight * 0.5));
    GUILayout.BeginArea ( Rect (ScreenX, ScreenY, areaWidth, areaHeight));

if(GUILayout.Button ("Play")) {
        OpenLevel ("AGP");
}
if(GUILayout.Button ("Instructions")) {
    OpenLevel ("Instructions_page");
}

if(GUILayout.Button ("Credits")) {
    OpenLevel ("Credits");
}

if(GUILayout.Button ("Quit")) {
        Application.Quit();
}   

GUILayout.EndArea();
}

    function OpenLevel (level : String) {
    yield new WaitForSeconds (0.35);
    Application.LoadLevel (level);

    }

Much kudos.

more ▼

asked May 17 '11 at 09:52 PM

Harley gravatar image

Harley
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Attach an AudioSource component to your script. Then write this code:

public AudioClip playClip;
public AudioClip instructionsClip;

if(GUILayout.Button ("Play"))
{
   audio.clip = playClip;
   audio.Play();
   OpenLevel ("AGP");
}
if(GUILayout.Button ("Instructions"))
{
    audio.clip = instructionsClip;
    audio.Play();
    OpenLevel ("Instructions_page");
}

Then you attach the sound file to your public playClip an instructionsClip.

Just add the rest in the same manner.

more ▼

answered May 17 '11 at 10:30 PM

ziC gravatar image

ziC
1 2 2 7

Okay, I will try this :)

May 17 '11 at 10:36 PM Harley

Sweet, i noticed if you want to play more then 1 sound at the same time with the same AudioSource. Use this instead: audio.PlayOneShot(TheClip);

May 17 '11 at 10:48 PM ziC
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3684
x788
x524
x386

asked: May 17 '11 at 09:52 PM

Seen: 1965 times

Last Updated: May 17 '11 at 09:52 PM