Reply To: On Start, anim is unassigned, so the line “anim = anim.GetComponent…” is assigning anim to nothing. The code works because we’re assigning anim manually in the Editor. Am I misunderstanding?

Forum Archive On Start, anim is unassigned, so the line “anim = anim.GetComponent…” is assigning anim to nothing. The code works because we’re assigning anim manually in the Editor. Am I misunderstanding? Reply To: On Start, anim is unassigned, so the line “anim = anim.GetComponent…” is assigning anim to nothing. The code works because we’re assigning anim manually in the Editor. Am I misunderstanding?

#26748
Jonathan Gonzalez
Participant

    I’m not sure why this was asked on this lesson as we didn’t directly use the Animator in any script. Nonetheless there are two ways of using GetComponent depending on what components you plan to use. If I write out:

    anim = GetComponent<Animator>();

    The script will expect this component to reside on the object that this script is applied to. If this component is found it will automatically assign it to “anim”. Otherwise it will be empty and will probably give you an error saying it can’t find that component.  

    The other way is to say specifically where this component will come from. Using the same example I can say:

    anim = anim.GetComponent<Animator>();

    By writing it out like that I’m saying that the component I want to use resides on the object assigned to “anim” in the variable above it. Either can be used, the first just automatically assigns it while the second requires us to manually assign an object with that component. If you have any other questions on this let me know.