Referencing components on the same object the script is on

will

Animator anim;

allow you to do

anim.SetBool(“Change”,true);

or do you have to go

public Animator anim;

then drag the object into the script slot for the animator

i think this will work, but is it the right way?

If you keep it private you’ll need to assign it via script. If public then you’ll need to assign it through inspector. If it’s on the same object use

anim = GetComponent<Animator>();

place that in your awake or start function. That will assign it if it is private.

To do the drag-drop method you do either one of these:

public Animator anim;
// OR
[SerializeField]
private Animator anim;

Alternatively you can use the GetComponent method @Boo-Let pointed out, usually inside of Awake().

1 Like