audio.Play(), me and my crappy logic

no damn clue why this isn’t working, everything i try results in failure.
i need heavyBreathing to play when fatigue is below 25, and breathing to play when it’s between 75 and 25

i confirmed that it IS changing the audio clips just fine by watching the inspector…

but NO MATTER WHAT. it NEVER plays heavyBreathing unless i remove the other if statement. what do?

var breathing : AudioClip;
var heavyBreathing : AudioClip;

static var currentaudioTime : float; // What the current time is.
static var audioLength : float; // The length of the clip.
static var timeLeft : float;
//static var timeLeftHB : int; // how much time remains in heavy breathing.
//static var timeLeftB: int; //how much time remains in breathing;



var Player : GameObject;
Player = GameObject.Find("Capsule");


var thirstyMouth : AudioClip;


function Update () 
{		
		timeLeft = audioLength - currentaudioTime;
		
     if (Player.GetComponent(Character_effects).fatigue <= 25  Player.GetComponent(Character_effects).fatigue >=0)
    {
		audio.clip = heavyBreathing;
		Debug.Log("time left: " + timeLeft);
	    audioLength =audio.clip.length;
	    currentaudioTime = audio.time;
		//timeLeftHB = audioLength - currentaudioTime;
		
			
	    if(timeLeft <= 0)
		{
        audio.Play();
		timeLeft = -1;
		}	
     }
	 
	 
	
	
	//the above heavy breathing script only functions when this lower one is disabled
	//this lower one originally played a more mild breathing sound when fatigue dropped below 75
	 
	 if (Player.GetComponent(Character_effects).fatigue <= 75  Player.GetComponent(Character_effects).fatigue >=25)
    {	    
	    audio.clip = breathing;
		Debug.Log("time left: " + timeLeft);
	    audioLength = audio.clip.length;
	    currentaudioTime = audio.time;
		//timeLeftB = audioLength - currentaudioTime;
		
		
	    if(timeLeft <=0)
		{
        audio.Play();
		}	
     }
}

I’d guess your second ‘if’ should be an ‘else if’.

The one thing i noticed is that in the above and below if statement, you ask if the fatigue is >= and <= 25. You should change one of those two to > or <. Right now, if fatigue is 25, your code is going to want to perform both actions. This would only be a problem when fatigue equals exactly 25 though. You should debug.Log the fatigue as well to make sure it’s not giving you 25 every frame or something like that.

Try these statements:

if(fatigue < 25){
	clip = heavyBreathing;
	if(timeleft < 0)
		clip.play();
} else if (fatigue < 75) {
	clip = breathing;
	if(timeleft < 0)
		clip.play();
}

all valid points i would have realized if i wasn’t retarded

and i think this fellow hit the nail on the head

i’ll try it first thing in the morning

damn
it didn’t work
it seems to be trying to play the file every single frame, and thus all i hear is a faint buzzing noise as the file restarts playing 30 times a second

Sorry I assumed that your logic with “timeleft” was sound. I hope you didn’t just copy the code I posted? It was more of a “hint” :wink: