randomizing my attack animation

All the pertinent code is below. I have a bunch of attack animations on a nicely rigged character. What I am trying to do is randomize the animations so that attacking is not so repetitive looking.

So, the issue is this. In PlayAttackSequence, whenever I randomly choose the index of my attack array like this
int i = Random.Range( 0, arrMeleAttacks.Length ); my attack animation gets all wierd and never plays correctly. If I use a hard coded value only, like 0, then it plays just fine. Can someone tell me why I cant get it to work with the random integer?

	IEnumerator Start () {
		while(true) {
			switch(_state) {
			case State.Init:
				Init();
				break;
			case State.Setup:
				SetUp();
				break;
			case State.Run:
				ActionPicker();
				break;
			}

			yield return null;
		}
	}
				
//Within the ActionPicker() is the following

                                if(_isAttacking)
				{
					attackTime+=Time.deltaTime;
					//Play Current Attack
					PlayAttackSequence();
					
					if(attackTime>=TotalAttackTime){
						_isAttacking=false;
						attackTime=0;
						TotalAttackTime = 0;
					}					
				}

//Then finally here is PlayAttackSequence()	
	void PlayAttackSequence()			
    {	
		int i = Random.Range( 0, arrMeleAttacks.Length );
		string attackName = arrMeleAttacks[0];
		animation[attackName].speed = animation[attackName].length / AttackSpeed;
		TotalAttackTime = animation[attackName].length;
		animation.CrossFade( attackName, 0.15f);			
    }

Your random value should be…

int i = Random.Range( 0, arrMeleAttacks.Length-1 );

The Random.Range is correct as is, the int version excludes max.
So on line 38 of your code, are you saying that if you replaced the “arrMeleAttacks[0];” with “arrMeleAttacks*;”, it doesn’t work?*

Yes, when I use i as the param, it suddenly starts freaking out…

You can see it here. The first few animations are a regular attack with a hard coded number. The second few attacks are when I use the random variable

Can’t watch the video yet, it’s still being processed.
I had one thought though, Could it be that the if block calling the PlayAttackSequence is being called from the Update loop?
You may want to pick one attackName when it enters the attack state and use that.

I think you were right Democre. I added a boolean flag (attackClicked) and now only call the function when its false. That allows whatever animation was chosen to complete before a new one is called. Thanks for the help!

				if(_isAttacking)
				{
								
					attackTime+=Time.deltaTime;
//					//Play Current Attack
					if(!attackClicked)
						PlayAttackSequence();
					
					if(attackTime>=TotalAttackTime){
						attackClicked = false;
						_isAttacking=false;
						attackTime=0;
						TotalAttackTime = 0;
					}					
				}