Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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>();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.