Animation.Play(useVar)

I want to make it random play animation per each level and difficulty level and make “animation.play(variable here)” like i make it shortcut variable… but look below:

Unity Error said that “The animation state could not be played because it couldn’t be found! Please attach an animation clip with the name ‘’ or call this function only for existing animations.”

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;
var lv1easy001 : AnimationClip;
var lv1easy002 : AnimationClip;
var lv1easy003 : AnimationClip;
var lv1easy004 : AnimationClip;
var lv1easy005 : AnimationClip;



function randomanim() {
    var randomanimation : int = Random.Range(1,5);
    print(randomanimation);



    if (leveldiff == lv1easy) //Level 1 Easy
    {
        num001 = lv1easy001;
        num002 = lv1easy002;
        num003 = lv1easy003;
        num004 = lv1easy004;
        num005 = lv1easy005;
        return;
    }


    //START ONCE!
    if (enemystartonce == true) {
        enemystartonce = false;
        animation.Play(num001);
        return;
    }

    yield WaitForSeconds(animation.clip.length);

    //START RANDOM ANIMATION
    if (randomanimation == 1)
    {
        animation.Play(num001);
        return;
    }
    else if (randomanimation == 2)
    {
        animation.Play(num002);
        return;
    }
    else if (randomanimation == 3)
    {
        animation.Play(num003);
        return;
    }
    else if (randomanimation == 4)
    {
        animation.Play(num004);
        return;
    }
    else if (randomanimation == 5)
    {
        animation.Play(num005);
        return;
    }

}

When you want to play specific animation, Animation.Play takes animation name as string for parameter. In your example, what type is “private var num001;” and where you tell the name of the animation you want to play?

yeah i did but i did type “num001 = lv1easy001;” then “animation.Play(num001);” that’s “lv1easy001 : AnimationClip;” then i got that error :frowning:

Ack.

Use arrays,
Explicitly type your variables, and
Use CamelCase for functions and camelCase for variables to make them readable.

static var levelName : String = "lv1easy";
var enemyStartOnce : boolean = true;
private var num : String[] = new String[6]; // this is a terrible name for a variable that isn't temporary; variables should express exactly what their function is
// Level 1 EASY
var lv1Name : String = "lv1easy";
var lv1EasyAnimations : String[] = new String[6];

function RandomAnim() {
    var randomAnimation : int = Random.Range(1,5);
    print(randomAnimation);

    if (levelName == lv1Name) //Level 1 Easy
    {
        num = new Array( lv1EasyAnimations ).ToBuiltin(String);
        return; // why?
    }

    //START ONCE!
    if (enemyStartOnce == true) {
        enemyStartOnce = false;
        animation.Play( num[ 1 ] );
        yield WaitForSeconds( animation[ num[1] ].length );
        return; // again, why?
    }

//    You never set animation.clip, this won't do anything you expect it to.
//    yield WaitForSeconds(animation.clip.length);

    //START RANDOM ANIMATION
    animation.Play( num[ randomAnimation ] );
}

hmm i dont understand that code… but where to put insert code for lv1norm (level 1 normal) and lv3easy (level 3 easy)?

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");
	}
}