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?

#26749
zeekar
Participant

    That’s bizarre – I intended for this question to be on the previous lesson – I’m not sure what happened there. Sorry about that.

    Regardless, thanks for answering. Here’s what I’m not clear on:

    Either can be used, the first just automatically assigns it while the second requires us to manually assign an object with that component.

    If the point of the second line of code is that you don’t want GetComponent to search the object the script is on for the Animator component, wouldn’t it make more sense to just not use either line of code? For example:

    #1

    public Animator anim;
    
    
    void Start (){
      anim = anim.GetComponent<Animator>();
    }

    VS:

    #2

    public Animator anim;
    
    
    void Start (){
    }

    Assuming that you are assigning anim manually in the editor, both #1 and #2 do the same thing. #2 just skips an unnecessary step. Does that make sense?