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?

posted to: Animation Events
  • Jonathan Gonzalez(jgonzalez) replied

    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. 

  • zeekar replied

    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?

  • demonslayer112 replied

    zzeekar  I am still starting to learn but I think I can answer this. 

    Even if you did manually assign Spike-trap to the variable in the inspector it doesn't know what Component to find. 

    I don't think it having the variable a Animator type is enough, like with referencing scripts like with the health script in this lesson you just create your own variable type by making it a "Health type" 


    Like I said I'm still learning this so I don't know if I'm right or not.

  • Jonathan Gonzalez(jgonzalez) replied

    Looks like Unity has finally changed how GetComponent works again. They've gone back and forth on this so I just naturally use GetComponent. Previously certain components like the Animator would require GetComponent even if you manually assigned it. So yes in this case writing that line wouldn't be necessary anymore unless you set it as private and want to assign it through script. Either way it's not going to cause issues. If you do encounter any "missing reference" errors that could be the case.

  • zeekar replied

    Thanks for clarifying