Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Game Development How to Make a Video Game Player Input and Cameras Move the Player with Animation

Yousef Almutairi
Yousef Almutairi
271 Points

what is the reason of this code?

void Start() { playerAnimator = GetComponent<Animator> (); }

can someone help me to understand this code, please? :)

3 Answers

The component "animator" should be attached to the player object. You can do this by looking at the object's inspector and clicking Add Component > Miscellaneous > Animator.

And indeed, you can name the variable "playeranimator" anything you want!

Yousef Almutairi
Yousef Almutairi
271 Points

Now all is clear, Thank You :)

craig ohanlon
craig ohanlon
2,694 Points

Create a variable called "playerAnimator" and get the <animator> component from the player object and assign it to "playerAnimator"

Yousef Almutairi
Yousef Almutairi
271 Points

Big thanks to you for answering me, so that component "animator" exist in a folder in unity, right? And I can name the variable "playerAnimator" to any different term, right ?

Susanna Remec
Susanna Remec
12,316 Points

At the start of the script you are creating a variable called playerAnimator and making it equal to the Animator component on your GameObject. This is how you can grab it and change it or access it in your code. Unity components are conveniently set up too be easy to find and change.