Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.