- Home /
Play multiple audio clips from one object
Hi there. I'm pretty new to Unity and C#, and am having trouble playing multiple sounds from one object.
The object is the player, and I had originally only attached the jump sound in the default audio slot of the player's audio source. Now, I also want to attach the coin-pickup sound to the player, since the coin is destroyed on pickup, and would not play this sound since the coin is already destroyed.
Is it possible to attach more than 1 audio clip to an object? If so, how would I do that? I only see one slot per audio source. Do I add more audio sources to the object?
I've tried quite a few tutorials instructing how to do this, but none of them seem to work. For example, currently I have:
[RequireComponent (typeof (AudioSource))]
then
public AudioClip jump;
public AudioClip pling;
and
void OnCollisionEnter2D (Collision2D col) {
Debug.Log ("collide coin");
if (col.gameObject.tag == "Coin") {
Debug.Log ("pling");
GetComponent<AudioSource> ().PlayOneShot (pling, 1);
}
}
Currently, I have two audio sources attached, each with either the "jump" or "pling" audio clips. Both "collide coin" and "pling" debug logs show up when I collect the coin, but no sound plays.
Thanks in advance for any help and suggestions!
Answer by Garazbolg · Jul 03, 2017 at 09:07 AM
You should only have one audio source. An audio source can only have one main (looping) sound, But it can play as many oneshot at the same time as you want. Your script should work, just be sure to have an audio source and have your pling clip set in your inspector.
Thanks for the reply @Garazbolg It's good to know the code should work fine.
How exactly do I set both the jump and pling audio clips to the audiosource in the Unity UI/Inspector. Is there a place where I drag both clips? I only see the one spot for the default looping clip.
Thanks again!
[Edited]
Never$$anonymous$$d! I didn't realize the audio could be attached via the script settings in the Inspector. Now it works perfectly.