Please explain playing audio clips in Unity 5

I’ve looked through the Unity documentation and watched videos trying to figure this out. With the deprecation in Unity 5 for a lot of the ways that this is done, YouTube videos and other sources aren’t that useful now.

Right now I just simply want to play a sound for an “explosion” and a “hit” when an enemy is destroyed or hit. There is an audio source component attached to the object that will play the sound (but I didn’t assign an audio clip to it because I wanted to use specific ones in the array).

I have an array:

public AudioClip audioClip;

Then with this I want to be able to PlaySound(0) or PlaySound(1).

I really don’t like the idea of being limited to one clip by attaching one to the Audio Source component. I want to be able to attach as many as I like to an array and play specific ones. I don’t know of any other way to have multiple sounds setup in the game object.

I tried different ways to play a sound in PlaySound() and have looked around for several days. I didn’t include the code for what is currently there because it doesn’t work and I made a mess of it trying different things. What code do I need to use inside of PlaySound()?

void PlaySound(int clip)
{

}

Can someone explain the way that this is done now in Unity 5?

You could probably do this:

void PlaySound(int clip)
{
    AudioSource source = GetComponent<AudioSource>();
    source.clip = audioClip[clip];
    source.Play();
}

The way we did it, was creating our own AudioPlayer script that creates a certain amount of GameObjects with AudioSource components attached to them. When another script needs to play a sound, it simply calls AudioPlayer.Play() with one or more clips. The AudioPlayer then takes one of its audio sources, assigns the clip and plays it. There’s a lot more it can do, but that’s its basic functionality.

I got that to work now. Thank you.