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!