Animation not found, but animation plays.

Hi, I have a script where an enemy is supposed to start a running animation when the player is in range, and move towards the player. I’ve got it so far that when I approach the enemy the running cycle works fine, and I get an error “Animation ‘enemyrun’ not found”, even though it’s clearly playing enemyrun. This means the rest of the script doesn’t work because it stops at the error, meaning the enemy never actually goes towards you, however stays in one place playing the run animation, and rotating to look at the player when the player moves.
I’ve set the animation type to legacy which got it to actually play, but I’m still getting the error. Here’s the code-

var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 110;

var TheDamage = 17;

private var attackTime : float;

var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;

    function Start ()
    {
    	attackTime = Time.time;
    }

    function Update ()
    {
	Distance = Vector3.Distance(Target.position, transform.position);
	
	if (Distance < lookAtDistance)
	{
		lookAt();
	}
	

	
	if (Distance < attackRange)
	{
		attack();
	}
	else if (Distance < chaseRange)
	{
		chase ();
	}
}

function lookAt ()
{
	GameObject.Find("hmmm").audio.Play();
	var rotation = Quaternion.LookRotation(Target.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}

function chase ()
{
	GameObject.Find("outsider").audio.Play();
	animation.Play("enemyrun");
	 
	moveDirection = transform.forward;
	moveDirection *= moveSpeed;
	
	moveDirection.y -= gravity * Time.deltaTime;
	controller.Move(moveDirection * Time.deltaTime);
}

function attack ()
{
	if (Time.time > attackTime)
	{
		animation.Play("enemyattack");
		Target.SendMessage("ApplyDamage", TheDamage);
		Debug.Log("The Enemy Has Attacked");
		
		attackTime = Time.time + attackRepeatTime;
	}
}

function ApplyDamage ()
{
	chaseRange += 30;
	moveSpeed += 2;
	lookAtDistance += 40;
}

thanks!

Try setting parameters in the Animator window and then making that parameter true in code.

You’ll need to make a variable for the Animator in code so you can access it like this

private var anim : Animator;

Then in the Start function bind that variable to the component on your enemy

function Start()
{
anim = GetComponent();
}

Then change your chase function to this

function chase()
{
    GameObject.Find("outsider").audio.Play();
    animation.Play("enemyrun"); //Replace this
    anim.SetBool("chase", true); //with this

 
    moveDirection = transform.forward;
    moveDirection *= moveSpeed;
 
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}

Then you’ll need to make a parameter in your animator window called chase and make it a type bool.

Long explanation but just in case you are not familiar with the Animator, so sorry if you are haha

Nice seeing you using one of Brackey’s tutorials. Link to it here guys:

Do you have a component called ‘Animation’ on the game object? Under Miscellaneous,(in the add component) click Animation. Then attach the animations.