# Unity GetCurrentAnimatorStateInfo

## 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 on other sites

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 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 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 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 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 on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628354
• Total Posts
2982229
• ### Similar Content

• By koto
Hello,

I'm developing a roguelike game. Currently it's in a playable state and I think it's a high time to show it and hopefully get some feedback.

The game has following features:
- turn based
- procedurally generated levels
- tons of loot
- spells system
- some quests
- some limited crafting
- door/key/lever mechanics
- quite a lot of different (look/behaviour) monsters

Trailer:
Regards,
Tom

• By Afambus
Hello,
I have a full experienced team that looking for a composer and another 3d designer, im expecting as much from you there are other people in this server that can help you we have a determined team, we have done many things in our game there is more information in the discord about everyone and there skills there region and about the game, if you join the discord pm asap
https://discord.gg/b2teN3m
• By BAG Labs
Mobile SoS

Platform: Android
Genre: Board

This games sharpen memory and test your strategies to place S-O-S pattern within time limit and serve 3 difficulties as Easy, Normal, and Hard.

Goals of the game is to put S-O-S words in patterns (Horizontal, Vertical, and Diagonal) alternately with enemy.

Features:

Screenshot:

• By abarnes
Hello!
I am a game development student in my second year of a three year program and I would like to start building my portfolio. I was thinking of creating some games to show what I can do to potential employers since I wont have any work related experience when I graduate. But as I'm sure you all know there are tons of ways to approach developing/designing a game and I'm curious if anyone had any insight as to any "standards" that come with this? Is it okay to use game engines like Unity, Unreal, Game Maker etc? Or would it be better to make a game from scratch to better show case your skills? Any and all advice will be greatly appreciated!
• By Hilster
Hello 2D Artists,
I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active.

The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
I intend to add more objects so the amount I'd like to add will be extended.
My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.