- Home /
Animator.GotoState: State could not be found.
Okay, so i got this code (C#)
using UnityEngine;
using System.Collections;
public class Stats : MonoBehaviour
{
public float Health;
public bool Dead;
private bool Invencible;
private float Counter;
private Animator Animator;
public float DeathCounter;
private bool AnimPlayed;
private bool MovementDisabled;
void Start ()
{
Animator = gameObject.GetComponent<Animator>();
DeathCounter = 6;
}
void Update()
{
if (Health > 100) { Health = 100; }
if (Health < 0) { Health = 0; }
if (Health == 0)
{
Die();
}
if (Invencible)
{
if (Counter <= 0)
{
Invencible = false;
}
else
{
Counter -= Time.deltaTime;
}
}
}
public void DealDamage (float Damage)
{
if(!Invencible)
{
Health -= Damage;
Invencible = true;
Counter = 1;
}
}
private void Die ()
{
if (!MovementDisabled)
{
Dead = true;
MovementDisabled = true;
}
if (DeathCounter <= 0)
{
Respawn();
}
else
{
if (DeathCounter <= 5)
{
DeathCounter = DeathCounter - Time.deltaTime;
if (!AnimPlayed)
{
AnimPlayed = true;
PlayAnim("Dead");
}
}
else
{
DeathCounter = DeathCounter - Time.deltaTime;
PlayAnim("Idle");
}
}
}
public void PlayAnim(string Name)
{
Animator.Play(Name, 0);
}
public void Respawn ()
{
}
}
And this animator (Please, tell me the animator navigation controlls)
But, anywhere in the code, when i try to play the animation "Dead" using
Animator.Play("Dead", 0);
it runs, and 2 seconds later (The code line is not even being executed)
Animator.GotoState: State could not be found UnityEngine.Animator:Play(String, Int32) Stats:PlayAnim(String) (at Assets/Systems/Character/Stat/Stats.cs:79) Stats:Die() (at Assets/Systems/Character/Stat/Stats.cs:67) Stats:Update() (at Assets/Systems/Character/Stat/Stats.cs:25)
The thing is, the animation plays fine. But the error makes me mad. With other animations it works perfect, and, i even tried playing the animation from other script, and, it don't shows any error. Is this a bug, or am i doing something wrong? Unity hates me. Thank you, and sorry for my english.
You are using the Class itself as variable..
Declaration should be:
private Animator animator; // use small a
Change all the variable names in the script to animator
To avoid any confusion you may choose a different name for the variable, say: myAnimator
Additional note: use camel casing for variables (eg: myAnimator, playerHealth, playerScore) and use pascal casing for function and class names (UpdateScore, RespawnEnemy)
Use any casing you like just don't use the same na$$anonymous$$g case-sensitive as any of all your classes that you have in your project.
P.S. You can also start your fields with underscore "_" character, just the tip.
It's a standard program$$anonymous$$g practice to use camel casing and pascal casing and putting it to practice will help others understand your code and help you understand other's code more easily. Besides no one uses '_' underscore for the properties. It's a bad program$$anonymous$$g practice. You may use them for accessors.
While the discussion of program$$anonymous$$g practice can be a good thing, in this case, it's really superfluous. I'm also running into this problem and the discussion is completely unhelpful - the OP is trying to figure out why they're getting an Animator.GotoState exception. They're not the only one.
Answer by 1MachoKualquiera · Jan 16, 2017 at 07:37 AM
As @sumeeton pointed out, my problem was naming the variable the same as its class name. This way, I was accidentally referencing a static Animator class that doesn't contain the animation state I was calling. This could be your issue too, @Asvarduil. Thank you guys ^^
Answer by Vollmondum · Oct 23, 2019 at 03:12 PM
Actual problem is RENAMING your animations and animation states in Mecanim. Make sure those coinside.