I want to get the current state from Animator. The reason I do this is because I want my sprite can approach multiple Idle state. For example, when sprite move left, when it go to idle state, it should go for leftidle instead of front idle. And

However when I type


"GetCurrentAnimatorStateInfo" the code is in red line which Visual Studios indicate that

'Game object' does not contain a definition GetCurrentAnimatorStateInfo and no extension method accpeting a first argument of type 'Game object' could be found (are you missing using a directive or an assembly reference)

Below I attach my code:

if (!Input.GetKey("up") && !Input.GetKey("down") && !Input.GetKey("left") && !(Input.GetKey("right") && (theanim.GetComponent<Animator>().GetBool("BackIdle") == true)))
            theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
            theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
            theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
            theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
            theanim.GetComponent<Animator>().SetBool(charactersidlewalk, false);
            theanim.GetComponent<Animator>().SetBool(charactersbackidle, true);
            theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
            theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);

Below I attach my Unity Animator:

enter image description here


First i apologize for my previous wrong answer ;) .

It was simple , to make an object work with other function , you need to take the component from it first , like you want to make a clay and water become vase then you need to get carving tools first .

What i was trying to tell is that your object don't have the " tools " to do your what you say , and to fix it , you must give it the " tools " by adding GetComponent<>() :

theanim.GetComponent<Animator> ().GetCurrentAnimatorStateInfo (0).IsName ("characterbackwalk");           

It is worked well here :) and if it solve your problem , then mark it ;) .

!! -- EDIT -- !!

I know your real problem , and i have tested that if GetCurrentAnimatorStateInfo will always return false in Update() , instead of using that , you can use GetBool() :


Then your script should be like this :

if(theanim.GetComponent<Animator> ().GetBool ("charactermovedown") == true ) { 

// do your function

  • \$\begingroup\$ I had edited my question code in above. It's ok. In this time I try to figure out the problem and get a solution. Thanks so much sincerely. ^^ \$\endgroup\$ – Philip Chow Nov 29 '16 at 16:21
  • \$\begingroup\$ are you trying to make idle state based at last direction ? \$\endgroup\$ – user6668201 Nov 30 '16 at 11:54
  • \$\begingroup\$ Yes, this is exactly what I want to achieve. For example if the character last move is moving left, when it go idle state, it should go for left idle instead of front idle \$\endgroup\$ – Philip Chow Nov 30 '16 at 12:03
  • \$\begingroup\$ Please use chat for further comments on this question: chat.stackexchange.com/rooms/49366/… \$\endgroup\$ – MichaelHouse Nov 30 '16 at 15:20
Animator m_anim = ...;
// Assert test passes only if I have the two  updates above.

and see this link:https://forum.unity3d.com/threads/getcurrentanimatorstateinfo-0-isname-returns-false-immediately-after-playing.224489/

  • \$\begingroup\$ I'm not understand with your reply. Forgive me for that. When I type theanim.GetCurrentAnimatorStateInfo(0) it is with red line, however when I type anim.GetCurrentAnimatorStateInfo(0), the code run but I do not get the result I want. So I wonder whether what I need to declare first before using GetCurrentAnimatorStateInfo \$\endgroup\$ – Philip Chow Nov 29 '16 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.