Animation Randomization Code is bogging down system

Well, I have, what I thought was, a simple animation script that randomizes which animation my character will perform during the Main Menu. However what’s happening is that over time, the CPU gets bogged down and animations begin to become jerky when they played (like watching it at 15 fps instead of 60)

The animations will play without jerkiness if they play first, but if they play some-odd seconds later, they become jerky (and, well, my fps drops like mad)

private var playedAnim;

function Start ()
{
	animation.wrapMode = WrapMode.Once;
	animation.Stop();
	animation.Play("yawn");
	playedAnim = 1;
	
}
function Update () {
animRand = ((Random.value)*5);

if (animRand < 1  playedAnim != 1)
	{
	animation.PlayQueued("yawn", QueueMode.CompleteOthers);
	playedAnim = 1;
	}
if (animRand > 1  animRand < 2  playedAnim != 2)
	{
	animation.PlayQueued("ridle", QueueMode.CompleteOthers);
	}
	if (animRand > 2  animRand < 3   playedAnim != 3)
	{
	animation.PlayQueued("sitdown", QueueMode.CompleteOthers);
	playedAnim = 3;
	}
	if (animRand > 3  animRand < 4   playedAnim != 4)
	{
	animation.PlayQueued("lookup", QueueMode.CompleteOthers);
	playedAnim = 4;
	}
	else if (animRand > 4  playedAnim != 5)
	{
	animation.PlayQueued("bobbing", QueueMode.CompleteOthers);
	playedAnim = 5;
	}
}

Any suggestions on how to clean it up and to get it to… I’m guessing stopping it from adding onto the stack of calls? I know that the animation Queue is getting huge due to the constant calls to add an animation to it, but I need it to play a random animation as soon as one’s finished.

I would probably do something like this? (not tested)

EDIT: I just noticed you had some logic for making sure the new animation is not the same as the previous animation, I obviously didn’t do that but you could probably figure out how to shoehorn it in.

private var animIsPlaying : boolean;

function Update() {
  	if(!animIsPlaying){
		PlayRandomAnim();
	}
}

function PlayRandomAnim(){
	animIsPlaying = true;
	var animRand = Random.Range(0,5);
	switch(animRand){
		case 0:
			animation.Play("anim0");
			yield WaitForSecond(animation["anim0"].length);
			break;
		case 1:
			animation.Play("anim1");
			yield WaitForSecond(animation["anim1"].length);
			break;
		case 2:
			animation.Play("anim2");
			yield WaitForSecond(animation["anim2"].length);
			break;
		case 3:
			animation.Play("anim3");
			yield WaitForSecond(animation["anim3"].length);
			break;
		case 4:
			animation.Play("anim4");
			yield WaitForSecond(animation["anim4"].length);
			break;
	}
	animIsPlaying = false;
}

Works just fine, but how do I get it to -NOT- play the same animation twice in a row with the switch statements? (As seen in the original script)

playedAnim != ## (## being 1 - 5)

I don’t want the character to play the yawn animation twice in a row for example

EDIT:
Eh, I’ll just use another if statement inside of the switch statement. Would rather not, but it works that way.

Maybe something like this would work? I never tried using a while loop like this, but it seems good in theory in my head (while animRand equals lastAnim, keep getting new random numbers until it isn’t anymore?):

private var animIsPlaying : boolean;
private var animRand : int;
private var lastAnim : int = -1;

function Update() {
  	if(!animIsPlaying){
		PlayRandomAnim();
	}
}

function PlayRandomAnim(){
	animIsPlaying = true;
	animRand = Random.Range(0,5);
	while(animRand == lastAnim)
		animRand = Random.Range(0,5);
	switch(animRand){
		case 0:
			animation.Play("anim0");
			yield WaitForSecond(animation["anim0"].length);
			break;
		case 1:
			animation.Play("anim1");
			yield WaitForSecond(animation["anim1"].length);
			break;
		case 2:
			animation.Play("anim2");
			yield WaitForSecond(animation["anim2"].length);
			break;
		case 3:
			animation.Play("anim3");
			yield WaitForSecond(animation["anim3"].length);
			break;
		case 4:
			animation.Play("anim4");
			yield WaitForSecond(animation["anim4"].length);
			break;
	}
	lastAnim = animRand;
	animIsPlaying = false;
}

Note that I initialized lastAnim at -1 so the first time the code is run lastAnim and animRand cannot be the same value.