1
\$\begingroup\$

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

enter image description here

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>();

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Your code looks okay. In the editor, have you clicked and dragged your AudioClips into the carSounds array? \$\endgroup\$ Mar 18, 2016 at 22:33
  • \$\begingroup\$ @Chris I have, I'll add a screenshot to the question \$\endgroup\$
    – Robert
    Mar 18, 2016 at 22:39
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Mar 18, 2016 at 22:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – Robert
    Mar 18, 2016 at 22:54
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Mar 18, 2016 at 22:56

1 Answer 1

2
\$\begingroup\$

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();
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ 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[]' \$\endgroup\$
    – Robert
    Mar 19, 2016 at 0:01
  • \$\begingroup\$ Ah, I forgot to cast in (AudioClip). I've updated the for loop now. \$\endgroup\$ Mar 19, 2016 at 0:49
  • \$\begingroup\$ Ah, that makes sense. So, why would carSounds be returning as Components if I told them to return as AudioClips? \$\endgroup\$
    – Robert
    Mar 19, 2016 at 0:56
  • \$\begingroup\$ I'm actually getting the same error though. \$\endgroup\$
    – Robert
    Mar 19, 2016 at 1:01
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Mar 19, 2016 at 3:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .