Cycle through animation clips using an index

I’m trying to develop a way of indexing all my animation clips to then cycle through and “preview” them in runtime.

  var index : int;
    var animations =  new Array();
    var character : GameObject;
    
    function Update (){
    	character = GameObject.Find("female");
    }
    
    function Start(){
    	index = 0;
    	for (var curAnimationState : AnimationState in character.animation){
    		temp  = curAnimationState.name;	
    		animations[index] = temp;
    		index++;
    	}
    	index = 0;
    }
    
    function OnMouseDown() {
    	//if (character.animation.isPlaying) {return;}
    	index = index%character.animation.GetClipCount();
    	var cur : String;
    	cur = animations[index];
    	character.animation.Play(cur);
    	index++; 
    }

I’m failing to see where im going wrong but I keep getting this following
error: ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index

hmm, it looks like its not adding your animations to the array.

You could try something like this and see if you have more luck, not sure if this works I do not have unity here and cant test it so its just a guess.

var myAnimations : Animation[] = [animation["walk"],animation["run"]];

var index : int = -1;

function OnMouseDown(){
index++
if(index > myAnimations.length-1)
{
index = -1;
}
else
{
animation.Play(myAnimations[index]);
}
}

I found a solution by using a string of the existing animation, this is no the most time friendly solution but it certainly provides a solution.

var c : GameObject;
var animNames: String[];
var i : int = 0;

function Update (){
	c = GameObject.Find("female");
}

function changePose() {
	i = (i + 1) % animNames.Length;
	if(c){
		c.animation.CrossFade(animNames*);*
  •  yield WaitForSeconds (c.animation.clip.length);*
    
  • }*
    }

function OnGUI(){

  • if (GUI.Button(Rect(10,290,127,30),“Change Animation”)){*
  •  changePose();*
    
  • }*
    }

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AnimTesterB : MonoBehaviour {

//Make sure there is no other animation script on the same gameojbect otherwise, this script will //be overriden by it
//also dont forget to include the above using statements, oherwise list wont work

public GameObject animatedObj;
//public string[] animations2;
public int animationIndex=0;
public List<string>animations = new List<string>();
private int totalAnimationsInTheObj=0;
	
	void Start () {

	if(animatedObj==null){Debug.Log ("Drag the animated obj to here");}
	totalAnimationsInTheObj=animatedObj.animation.GetClipCount();

		foreach(AnimationState state in animation){
			animations.Add (state.name);
			
		}

	}
	

	IEnumerator changePose () {

	animationIndex=(animationIndex+1)%animations.Count;
	if(animatedObj){
		animation.CrossFade(animations[animationIndex]);
	yield return new WaitForSeconds(animatedObj.animation.clip.length);
	}
}
void OnGUI(){

	if(GUI.Button(new Rect(10,250,150,40),"Change Animation")){
		StartCoroutine(changePose ());
	}
}

}

Just posted a simple and easy to follow script on a similar question.

http://answers.unity3d.com/questions/8245/how-to-select-an-animation-clip-by-index-number.html?page=2&pageSize=5&sort=votes