# Why should I use getcomponent?

I have already referred the animator by using the statement,

       Animator anim;


But,What is the use of getcomponent in awake() function.What happens without it.

## 2 Answers

Animator anim; is just a empty reference. If you want to actually access the Animator script attached to the object, you'll have to use GetComponent<Animator>() and assign it to anim.

Your first sentence is incorrect Animator anim; does not refer to anything. It's simply a placeholder for referencing an Animator object. It doesn't point to anything unless you tell it what to point to.

if you make a public variable like

public Animator anim;


in a monobehavior script, outside any method, you can drag and drop a gameobject with animator component in inspector when you add this script to a gameobject. it will automatically get the Animator component from the dropped gameobject.

But if you already have a reference to the desired gameobject(g) with a Animator component, you can get a reference to it by

anim= g.GetComponent<Animator>();