# Playing multiple sounds in Unity5

I have multiple AudioClips that I have attached to a GameObject and I'm trying to play them when certain conditions are met. Each clip is in an array and I have each array item attached to a variable. I'm also getting the GameObject's Audio Source. My problem is, I can't get the clips to actually play, how do I get them to do so?

C#

private Rigidbody playerCar;
private AudioSource engine;
public AudioClip[] carSounds = new AudioClip[3];
AudioClip accelerate;
AudioClip toppedOut;
AudioClip idle;
private bool accelerating;
private bool idling;
private bool maxed;

void Start(){
playerCar = GetComponent<Rigidbody>();
engine = playerCar.GetComponent<AudioSource>();
carSounds = playerCar.GetComponents<AudioClip>();
idle = carSounds[0];
accelerate = carSounds[1];
toppedOut = carSounds[2];
idling = true;
}

void Update(){
if(idling){
engine.PlayOneShot(idle);
accelerating = false;
maxed = false;
}
}


Editor

For further clarification, here is the exact error Unity is giving me.

ArgumentException: GetComponent requires that the requested component 'AudioClip' derives from MonoBehaviour or Component or is an interface. UnityEngine.GameObject.GetComponents[AudioClip] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:100) UnityEngine.Component.GetComponents[AudioClip] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineComponentBindings.gen.cs:183) MoveCar.Start () (at Assets/Scripts/MoveCar.cs:28)

Line 28 is carSounds = playerCar.GetComponents<AudioClip>();

• Your code looks okay. In the editor, have you clicked and dragged your AudioClips into the carSounds array? – Chris McFarland Mar 18 '16 at 22:33
• @Chris I have, I'll add a screenshot to the question – Robert Mar 18 '16 at 22:39
• Just to rule out there isn't an issue with PlayOneShot, could you try engine.clip = idle; engine.Play();? Does this play the clip? Is the "Mute Audio" button below the Game tab is pressed? Does your computer play other sounds okay? Have you tried closing and re-opening Unity? Restarted your computer? – Chris McFarland Mar 18 '16 at 22:52
• I've tried setting the clip the way you mention. The computer has no issues playing other sounds, I'm even able to hear it when I just play the clip from the editor. I haven't tried restarting Unity though, I'll give that a shot. I also updated the question with more details. – Robert Mar 18 '16 at 22:54
• Ah, thanks for posting the error. Error messages help solve things. GetComponents returns Component[] I believe, which is not AudioClip[]. Let me write up an answer. – Chris McFarland Mar 18 '16 at 22:56

Big edit:

From chatting and working with Robert on this, to resolve this issue, the solution seems to be:

1. Remove the carSounds = playerCar.GetComponents<AudioClip>(); line because GetComponents<AudioClip>() will always return a null array because AudioClips aren't components that can be added to GameObjects. This was overwriting the AudioClips that had already been assigned in the Inspector tab.

2. PlayOneShot just doesn't seem to work. Perhaps an issue with Unity?

3. Add in extra code to assign engine's AudioClip and tell it to Play.

Working code:

if (idling) {
engine.loop = true;
engine.clip = carSounds[0];
if (!engine.isPlaying) {
engine.Play();
}
}

• Now I'm getting an error saying that I can't implicitly convert the AudioClip type to a Component. Here's the exact wording. Assets/Scripts/MoveCar.cs(28,21): error CS0029: Cannot implicitly convert type UnityEngine.AudioClip[]' to UnityEngine.Component[]' – Robert Mar 19 '16 at 0:01
• Ah, I forgot to cast in (AudioClip). I've updated the for loop now. – Chris McFarland Mar 19 '16 at 0:49
• Ah, that makes sense. So, why would carSounds be returning as Components if I told them to return as AudioClips? – Robert Mar 19 '16 at 0:56
• I'm actually getting the same error though. – Robert Mar 19 '16 at 1:01
• @Chris If you call PlayOneShot before previous play is finished, it cuts previous one immediately and starts new one. So, if you call it on Update(), you cannot hear anything, because it start audio clip over and over again at every frame. – S. Tarık Çetin Mar 19 '16 at 3:55