- Home /
play audio clip
Hi, hoping someone can help out here!!! i have a script for GUI buttons, working fine... but what i need it to do is to play a diffrent audio clip for each button.. i.e button 1 when clicked it plays one sound, and when button 2 is clicked it plays a diffrent sound etc., all help would be super appreciated, thanx in advance heres the script i`m working on..
var cubeRenderer:Renderer;
var buttonWidth:int = 200;
var buttonHeight:int = 50;
var spacing:int = 25;
var mySound : AudioClip;
var mySkin : GUISkin;
function OnGUI() {
GUI.skin = mySkin;
if(GUI.Button(Rect(Screen.width/2 -buttonWidth/2, Screen.height/8 -buttonHeight/1 - spacing, buttonWidth, buttonHeight), ""))
{
audio.Play();
}
if(GUI.Button(Rect(Screen.width/1.4 -buttonWidth/3, Screen.height/8 -buttonHeight/1 - spacing, buttonWidth, buttonHeight), ""))
{
audio.Play();
}
if(GUI.Button(Rect(Screen.width/3 -buttonWidth/1, Screen.height/8-buttonHeight/1 - spacing, buttonWidth, buttonHeight), ""))
{
audio.Play();
}
}
ok i have gone aboout it a diffrent way but am now pushing to many GUI`s..lol and still cant figure out how to make the sound play after the button is pushed!!! any help would be good... new script :
var buttonWidth:int = 200;
var buttonHeight:int = 50;
var spacing:int = 25;
//var mySound : AudioClip;
var mySkin : GUISkin;
function OnGUI () {
GUI.skin = mySkin;
// Starts an area to draw elements
GUILayout.BeginArea (Rect (100,100,400,400));
GUILayout.Button ("ONE");
GUILayout.Button ("TWO");
GUILayout.Button ("THREE");
GUILayout.Button ("FOUR");
GUILayout.Button ("FIVE");
GUILayout.EndArea ();
GUILayout.BeginArea (Rect (600,100,400,400));
GUILayout.Button ("SIX");
GUILayout.Button ("SEVEN");
GUILayout.Button ("EIGHT");
GUILayout.Button ("NINE");
GUILayout.Button ("TEN");
GUILayout.EndArea ();
GUILayout.BeginArea (Rect(Screen.width/1.2 -buttonWidth/2, Screen.height/1.2 -buttonHeight/1 - spacing, buttonWidth, buttonHeight));
if(GUILayout.Button("
{
Application.LoadLevel("");
GUILayout.EndArea ();
}
}
Answer by DaveA · Aug 02, 2013 at 02:55 PM
Have a different AudioCLip variable for each separate sound (or an array of them), assign via Inspector. THen put audio.clip = whateverCLip before each audio.Play()
Your answer
![](https://koobas.hobune.stream/wayback/20220613111425im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Play sound on button click before loading level. 3 Answers
Help With GUI Stop and play other Sound ? 1 Answer
Play Sound when GUI button is pressed. 7 Answers
GUI.Button Hover = Sound 1 Answer
Unity only one sound works per scene 2 Answers