audioclips, how to play them?

trying to make the clip BreathingNormal loop continuously at all times “fatigue” is above 75
but when it drops below 50, to play BreathingFast

except the compiler keeps barking that .Play() is not a member of audioclip…well then how to hell do you make it play!?

var breathing : AudioClip;
var heavyBreathing : AudioClip;
var thirstyMouth : AudioClip;


function Update () {

if (gameObject.GetComponent(Character_effects).fatigue <= 75)
   {
   yield WaitForSeconds (audio.clip.length);
   audio.clip = breathing;
   breathing.Play(44100);
   }

if(gameObject.GetComponent(Character_effects).fatigue <= 25)
{
yield WaitForSeconds (audio.clip.length);
audio.clip = heavyBreathing;
heavyBreathing.Play(44100);
}


}

Create an empty game object and attach an audio source (component >> audio >> audio source). Set your audio clip in the inspector.

In another script, do this:

var sound : AudioSource; //drag audio source onto this in inspector

function Update(){
	if (Input.GetKeyDown("space")) sound.Play();
}

You need an AudioSource to be able to play audio.

Here’s what I do (I normally code in C#, so I’m sorry if my javascript syntax is rusty).

  1. Add an audio source component. You can do this in the inspector or in code. I do it in the start function like below.
  2. you can access this audio source with the instance ‘audio’.
  3. Set the clip using audio.clip
  4. Play the clip using audio.Play()
var breathing : AudioClip;
var heavyBreathing : AudioClip;
var thirstyMouth : AudioClip;

function Start()
{
    //Add an AudioSource component (you might want to check if one already exists first)
    gameObject.AddComponent(AudioSource);
}

function Update () 
{

    if (gameObject.GetComponent(Character_effects).fatigue <= 75)
    {
        audio.clip = breathing;
        audio.Play();
        yield WaitForSeconds (audio.clip.length);

     }

     if(gameObject.GetComponent(Character_effects).fatigue <= 25)
     {
        audio.clip = heavyBreathing;                
        audio.Play(44100);
        yield WaitForSeconds (audio.clip.length);

     }


}

neither of those implementations are working
they don’t give compiler errors but they aren’t playing while in the scene for some reason

edit; wait, hold up, my ifs are not working

ok i got it working after realizing i had to drag the actual audio source component from the inspector into the script’s waiting audio source field, NOT drag the audio file there

now the problem is that it plays the file on every frame, instead of just starting.
i assume that’s just a job for audioclip.length to make sure its done playing?

edit: wait what the hell? now it’s giving a constant error that “update() is not a coroutine” and will not run anything within update

ah, i get it
the yield WaitForSeconds call cannot be done within update, as update cannot pause for anything.

but if that’s the case, how do you make it so that the audioclip plays completely before looping again or changing to a different clip?

static var currentTime : int; // What the current time is.
static var songLength : int; // The length of the song.
static var timeLeft : int; // how much time remains.

function Update() {
songLength = audio.clip.length;
currentTime = audio.time;

timeLeft = songLength - currentTime;

if (timeLeft <= 0) {
//Do whatever you want to do when the song is over.
}
}

There’s probably a better way, but it works.