There are many videos, tips and tricks in the internet to demonstrate how to use more than one Audiosource in Unity 3D – most of them show not the best way to do this.

So here is a simple and in my opion right way to add multiple AudioSource-components to a Script:

Add the Audiosources

Click on the button Add Component for your gameobject and add an Audio Source component:

Add_AudioSource

Assign the audioclip you want to use to this component:

Audiosource Footsteps Unity3d

After this, do the same or the next Audiosource:

Audiosource Monster Grunt Unity3d

Add Audiosources in C# script

Add a script to your gameobject and define the following Array:

public AudioSource[] AudioClips = null;

After that switch to the gameobject in Unity (Inspector Window), set the size of the Audio Clips-array to 2 and add the Audiosources via Drag n Drop to the elements:

AudioSource_array

Play the Clips

In your code you can play the audioclips like this:


// This is an example of a footsteps clip when the character is walking
// (in the Update method)
_animator.SetBool("dowalk", true);

// It's good to add a reminder which item refers to which clip
// 0 = Footsteps
// 1 = Monster Grunt
if (!AudioClips[0].isPlaying)
{
  AudioClips[0].Play();
}

// Play the other audioclip when you need it
if (!AudioClips[1].isPlaying)
{
  AudioClips[1].Play();
}

That’s it, hope you like it, it’s simple if you know how to do it.

(Visited 196 times, 1 visits today)