- Home /
Trigger diferent sounds playing from the same object
Hello! My objective is to have 2 buttons, each button makes one object (cube) play a different animation and at the same time it plays a sound for each animation.
For now I have a cube with animations and two Audio Sources in the cube (called 01 and 02) I´m trying to assign each sound to a variable but I don´t know how to do it. If I write 01 it´s a int, "01" string, how to say it´s an AudioSource?
using UnityEngine;
using System.Collections;
public class buttons : MonoBehaviour {
void OnGUI () {
// Make a background box
GUI.Box(new Rect(10,10,100,150), "Animations");
// sounds
AudioClip sound1 = 01; // <- i´m guessing my problem is in these lines...
AudioClip sound2 = 02;
// Make the first button
if(GUI.Button(new Rect(20,40,80,20), "Animation 1")) {
animation.Play("take_01");
//audio.Play();
audio.PlayOneShot(sound1, 1.0F);
}
// Make the second button.
if(GUI.Button(new Rect(20,70,80,20), "Animation 2")) {
animation.Play("take_02");
audio.PlayOneShot(sound2, 1.0F);
}
}
}
Answer by golbit · Feb 20, 2013 at 05:11 PM
Ok, I solved it :)
Instead of working directly with the object I created a prefab and dragged the script there. The public var let me drag the sounds to it and it became very easy to make it working.
using UnityEngine;
using System.Collections;
public class buttons : MonoBehaviour {
public AudioClip[] effects;
void OnGUI () {
// Make a background box
GUI.Box(new Rect(10,10,100,150), "Animations");
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if(GUI.Button(new Rect(20,40,80,20), "Animation 1")) {
animation.Play("take_01");
audio.PlayOneShot(effects[0], 1.0F);
}
// Make the second button.
if(GUI.Button(new Rect(20,70,80,20), "Animation 2")) {
animation.Play("take_02");
audio.PlayOneShot(effects[1], 1.0F);
}
// Make the third button.
if(GUI.Button(new Rect(20,100,80,20), "Animation 3")) {
animation.Play("take_03");
audio.PlayOneShot(effects[2], 1.0F);
}
// Make the fourth button.
if(GUI.Button(new Rect(20,130,80,20), "Animation 4")) {
animation.Play("take_04");
audio.PlayOneShot(effects[3], 1.0F);
}
}
}
Your answer
Follow this Question
Related Questions
Audio Play Once 2 Answers
why not run animation when doublication gameObject ???? 0 Answers
Audio Scripting 1 Answer
Animation Scripting Help #C 1 Answer
Play sound on animation event? 3 Answers