GetCurrentAnimatorStateInfo

Started by
5 comments, last by Howgyn 7 years, 4 months ago
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'?
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.

@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);
        }
}

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.

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.

'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.

Ok now I understand what you said. Can you suggest anyway how I can edit my code so it work?

This topic is closed to new replies.

Advertisement