- Home /
Question by
Matheus_Aguilera · Dec 04, 2017 at 07:49 PM ·
animatoranimator controlleranimationsexception
KeyNotFoundException in animator
Hello everyone.
I am trying to animate a sprite while it moves towards the player in Unity 2017.1.1f1.. After editting the sprites, creating the animations and coding I tested the game and it worked perfectly. However, after making a few changes that I had to undo because they weren't working as I wanted them to, I tested again and noticed that the horizontal walking animation wasn't working anymore. When I opened the animator tab there was this message on the console tab:
KeyNotFoundException:The given key was not present in the dictionary. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
Here's a screenshot:
And here's the code, in case it's a programming issue:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControls : MonoBehaviour {
public Transform target;
Animator anim;
int direction, state;
float speed, timer;
void Move()
{
//horizontal movement: move left
if (transform.position.x - target.position.x > 0.3f) {
direction = 1;
state = 1;
anim.SetInteger ("direction", direction);
anim.SetInteger ("state", state);
transform.Translate (-transform.right * speed * Time.deltaTime);
}
//horizontal movement: move right
else if (transform.position.x - target.position.x < -0.3f) {
direction = 3;
state = 1;
anim.SetInteger ("direction", direction);
anim.SetInteger ("state", state);
transform.Translate (transform.right * speed * Time.deltaTime);
}
//horizontal movement: stop
else {
state = 0;
anim.SetInteger ("state", state);
}
//vertical movement: move down
if (transform.position.y - target.position.y > 0.3f) {
direction = 0;
state = 1;
anim.SetInteger ("direction", direction);
anim.SetInteger ("state", state);
transform.Translate (-transform.up * speed * Time.deltaTime);
}
//vertical movement: move up
else if (transform.position.y - target.position.y < -0.3f) {
direction = 2;
state = 1;
anim.SetInteger ("direction", direction);
anim.SetInteger ("state", state);
transform.Translate (transform.up * speed * Time.deltaTime);
}
//vertical movement: stop
else {
state = 0;
anim.SetInteger ("state", state);
}
}
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
target = GameObject.FindWithTag ("Player").transform;
speed = 1.0f;
state = 0;
}
// Update is called once per frame
void Update ()
{
Move ();
}
}
I appreciate any help you can provide.
erro-1.png
(112.0 kB)
Comment