Jump to content
  • Advertisement
Sign in to follow this  
Howgyn

Unity GetCurrentAnimatorStateInfo

This topic is 596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to get the current state from Unity Animator. However when I type theanim.GetCurrentAnimatorStateInfo(0).IsName("charactersbackwalk"), the code "GetCurrentAnimatorStateInfo" is in red line which Visual Studios indicate that "'Game object' does not contain a definition GetCurrentAnimatorStateInfo and no extension method accepting a first argument of type 'Game object' could be found (are you missing using a directive or an assembly reference)" When I type anim.GetCurrentAnimatorStateInfo(0).IsName("charactersbackwalk"), the code run but the result is not what I want. Therefore, I wonder it is necessary to declare something before using 'GetCurrentAnimatorStateInfo'?

Share this post


Link to post
Share on other sites
Advertisement

Firstly, if the code compiles and runs, you can ignore what Visual Studio tells you - sometimes the highlighting is wrong.

 

However, I'd be surprised to see the code run at all since I believe the message is correct - the method you're using is part of the Animator component, not a GameObject. You need to refer to that component, not a game object. If you are saying that changing from "theAnim" to "anim" makes the code compile and run but the answers are not what you expect, there might just be a bug elsewhere in your code.

 

If that isn't enough to get going, I suggest posting your code.

Share this post


Link to post
Share on other sites

@Kylotan

Here I attached my code below: 
(GetCurrentAnimatorStateInfo) get red line and can't compile in Unity

void Update() 
{
    GameObject characters = GameObject.Find(charactersname);
    GameObject theanim = GameObject.Find(charactername);

    if (!Input.GetKey("up") && !Input.GetKey("down") && !Input.GetKey("left") && !(Input.GetKey("right")) && (theanim.GetCurrentAnimatorStateInfo(0).IsName("charactersbackwalk")))
        {
            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);
        }
}
Edited by Howgyn

Share this post


Link to post
Share on other sites

You're referring to something called 'anim'. You've not created or declared that object in the code you've posted. The error message suggests that it's a GameObject, which does not contain that method. You probably want to replace that with 'theanim.GetComponent<Animator>()' just like you're doing in the code below it.

Share this post


Link to post
Share on other sites

Very sorry though ... It is my mistake. 
 

I mistakenly wrote it as anim, actually what I want to wrote is (theanim.GetCurrentAnimatorStateInfo(0).IsName("charactersbackwalk"))
"theanim" make "
GetCurrentAnimatorStateInfo" become red line.

 

Share this post


Link to post
Share on other sites

'Theanim' is a GameObject, so what I said above still applies. You need the relevant Animator component, just like you do inside the if statement.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!