0
\$\begingroup\$

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:

enter image description here

\$\endgroup\$
1

2 Answers 2

1
\$\begingroup\$

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() :

https://docs.unity3d.com/ScriptReference/Animator.GetBool.html

Then your script should be like this :

if(theanim.GetComponent<Animator> ().GetBool ("charactermovedown") == true ) { // do your function } 
\$\endgroup\$
4
  • \$\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\$ Commented Nov 29, 2016 at 16:21
  • \$\begingroup\$ are you trying to make idle state based at last direction ? \$\endgroup\$ Commented Nov 30, 2016 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\$ Commented Nov 30, 2016 at 12:03
  • \$\begingroup\$ Please use chat for further comments on this question: chat.stackexchange.com/rooms/49366/… \$\endgroup\$ Commented Nov 30, 2016 at 15:20
0
\$\begingroup\$
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")); 

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

\$\endgroup\$
1
  • \$\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\$ Commented Nov 29, 2016 at 11:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.