In canvas, I have attached few buttons. Each button has button script. I have imported some audio and there is a game object audio in that scene which has audio script.
Now, I press a button, how can i play the audio? If i copy those scripts as a single one, i can make it play.
But how do i keep them separately and make them to play?
public class YourName : MonoBehaviour {
private AudioSource source;
public AudioClip clip;
// Use this for initialization
void Start () {
source = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Your button")) // Button can be setup in Edit->ProjectSettings->Input
{
source.PlayOneShot(clip);
}
}
}
And don’t forget to add Audio Source in Add Component in Inspector