For a specific animation, you can call the animation with play as well.
// Plays the walk animation - stops all other animations in the same layer
animation.Play("walk");
If you want to say that walk-animation in the variable that you can edit in editor:
var MyWalkAnimationName : String = "walk";
function PlayWalkAnimation()
{
animation.Play(MyWalkAnimationName);
}
If you want to play one random walk animation from the list of strings you can use
var MyWalkAnimations = {"walk","SillyWalk","walk2","moonwalk"};
function PlayRandomWalkAnimation()
{
animation.Play(MyWalkAnimations[Random.Range(0,MyWalkAnimations.length)]);
}
Now in your code
//FOR ANIMATION
static var leveldiff : String = "lv1easy";
private var num001;
private var num002;
private var num003;
private var num004;
private var num005;
var enemystartonce : boolean = true;
//ANIMATION LIST:
// Level 1 EASY
var lv1easy; //Here we define variable. Unless we give it value somewhere else, it will be null.
//Define 5 Animation Clips.
var lv1easy001 : AnimationClip;
var lv1easy002 : AnimationClip;
var lv1easy003 : AnimationClip;
var lv1easy004 : AnimationClip;
var lv1easy005 : AnimationClip;
function randomanim() {
var randomanimation : int = Random.Range(1,5); // You never get number 5 since [URL="http://unity3d.com/support/documentation/ScriptReference/Random.Range.html"]Random.Range(int,int)[/URL] is max exclusive.
print(randomanimation);
if (leveldiff == lv1easy) //Level 1 Easy //This will be never true since ("lv1easy"==null) is not true.
{
//Now we copy 5 animation clips in to other places. They still are animation clips.
num001 = lv1easy001;
num002 = lv1easy002;
num003 = lv1easy003;
num004 = lv1easy004;
num005 = lv1easy005;
return; // And in case of that if is somehow true, never do rest of this function.
}
//START ONCE!
if (enemystartonce == true) {
enemystartonce = false;
animation.Play(num001); // num001 is animation clip, not a String.
return;
}
yield WaitForSeconds(animation.clip.length);
//START RANDOM ANIMATION
if (randomanimation == 1)
{
animation.Play(num001);// num001 is animation clip, not a String.
return;
}
else if (randomanimation == 2)
{
animation.Play(num002);// num002 is animation clip, not a String.
return;
}
else if (randomanimation == 3)
{
animation.Play(num003);// num003 is animation clip, not a String.
return;
}
else if (randomanimation == 4)// num004 is animation clip, not a String.
{
animation.Play(num004);
return;
}
else if (randomanimation == 5)// num005 is animation clip, not a String.
{
animation.Play(num005);
return; / and you are doing this inside else if, Personaly I think return here is bit overkill.
}
}
I think (but I might be wrong since I’m making a very strong assumptions here) that what you tro to accomplish here is:
var EnemyStartOnce : boolean = true;
var lv1easy001 : AnimationClip;
var lv1easy002 : AnimationClip;
var lv1easy003 : AnimationClip;
var lv1easy004 : AnimationClip;
var lv1easy005 : AnimationClip;
function Start()
{
//Since [URL="http://unity3d.com/support/documentation/ScriptReference/Animation.html"]Animation [/URL] doesn't know anythina about our 5 clips we need to load them first.
[URL="http://unity3d.com/support/documentation/ScriptReference/Animation.AddClip.html"]animation.AddClip[/URL](lv1easy001 , "lvl1EasyClip001");
animation.AddClip(lv1easy002 , "lvl1EasyClip002");
animation.AddClip(lv1easy003 , "lvl1EasyClip003");
animation.AddClip(lv1easy004 , "lvl1EasyClip004");
animation.AddClip(lv1easy005 , "lvl1EasyClip005");
}
function PlayRandomAnimation()
{
var i = 1;
if(EnemyStartOnce)
{
EnemyStartOnce = false;
}
else
{
i = Random.Range(1,6);
}
print("Random animation number " + i + " will be played");
if(i == 1)
{
animation.Play("lvl1EasyClip001");
}
else if(i == 2)
{
animation.Play("lvl1EasyClip002");
}
else if(i == 3)
{
animation.Play("lvl1EasyClip003");
}
else if(i == 4)
{
animation.Play("lvl1EasyClip004");
}
else if(i == 5)
{
animation.Play("lvl1EasyClip005");
}
}