GetCurrentAnimatorStateInfo(0)

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

theanim.GetCurrentAnimatorStateInfo(0).IsName("charactersbackwalk"))


"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:

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 ) {

}

• 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. ^^ – Philip Chow Nov 29 '16 at 16:21
• are you trying to make idle state based at last direction ? – user6668201 Nov 30 '16 at 11:54
• 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 – Philip Chow Nov 30 '16 at 12:03
• Please use chat for further comments on this question: chat.stackexchange.com/rooms/49366/… – MichaelHouse Nov 30 '16 at 15:20
Animator m_anim = ...;
m_anim.Play("GenericSpinReveal");
m_anim.Update(Time.smoothDeltaTime);
m_anim.Update(Time.smoothDeltaTime);
// Assert test passes only if I have the two  updates above.
DebugUtils.Assert(m_anim.GetCurrentAnimatorStateInfo(0).IsName("Base.GenericSpinReveal"));

• 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 – Philip Chow Nov 29 '16 at 11:44