accesing animations by thier array number

Hi there,
I was just wondering if it was possible to access animations by their array number instead of their name?

so instead of - Character.animation.Play(“Run”);
you might have something like - Character.animation.Play[1];

I was trying to set up a scene where I could go through and check the cylces on characters quickly and it would be nice to have a var that i could just flick between.

Like this -
var currentanimation : int
Character.animation.Play[currentanimation];
then i could just click throught them in the inspector

Thanks
Pete

You can obtain the names of all animations by using:

for (var state : AnimationState in animation)
    Debug.Log(state.name);

(untested).

If you add these names into an array, you can indirectly access the individual animations by index (i.e. the index into your array of names).

Hey thanks,

That works but I have a little trouble with arrays,
I cant seem to add to them, not too sure what I am doing wrong(probably something kinda basic)

var Character : GameObject;
var AnimationNames : String[];

function Start(){
	for (var state : AnimationState in Character.animation){
	AnimationNames.Push(state.name);
    Debug.Log(state.name);
	}
}

This seems to grab the animation names fine but I can’t seem to get them to add to the “AnimationNames” Array.
Do you know what I might be doing wrong there?

I keep getting
‘Push’ is not a member of ‘(String)’.

Thanks
Pete

You are using a ‘built-in’ array, which is fast, but doesn’t allow any dynamic adding/removing of objects. Functions like push and pop do not exist for it.

However, in this case you know what size it will be, so you can use it like this:

   var cnt = 0;
   AnimationNames = new String[Character.animation.GetClipCount()];
   for (var state : AnimationState in Character.animation)
   {
      AnimationNames[cnt] = state.name;
      cnt++;
   }

(untested)

In situations where you do need dynamic arrays, use the Array class:
http://unity3d.com/support/documentation/ScriptReference/Array.html

This class does implement functions like Push() but the arrays are declared and created different from built-in arrays (as described on the page).

Hey tomvds!
That works and now I get what I’ve been doing wrong with arrays!

Thanks
Pete

Hi there- I know I’m resurecting an old and dead post, but I just can’t figure out how to use this.

I have the same issue, I would like to play my animations based on a random number and then use that to choose the anim.

So, I have this so far:

var evasiveAnimations :  Animation[];
var minDexterity : int = 0;
var maxDexterity : int = 9;
function EvasiveAction()
{
	evasiveMode = true;
	aggressiveMode = false;
	var evasiveChoice = Random.Range(minDexterity,maxDexterity);
	gameObject.animation.Play(evasiveAnimations[evasiveChoice]);
}

As you discovered, this doesn’t work…but I can’t quite figure out how to implement your solution :frowning:

Any help would be greatly appreciated, thanks!

Do you have your animations stored in the “evasiveAnimations” array?
That looks like it should work to me, are you getting an error or is it just not doing anything?

Hey Petey- thanks for the reply.

Yeah, I have them in the array, but then when i add elements to the aray via inspector, I can’t actually choose any of my animations…it just shows little “clock” symbols and some other files. Strange.

I am now trying to add an Animation component, then add in the animations to each of the elements in there, which works, however I have no idea how to call them to be played…arg.