Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Animator component detaches itself from script at runtime

Discussion in 'Scripting' started by lucaskargreen, Sep 16, 2020.

  1. lucaskargreen

    lucaskargreen

    Joined:
    Sep 10, 2020
    Posts:
    21
    upload_2020-9-16_18-46-7.png

    upload_2020-9-16_18-47-43.png

    upload_2020-9-16_18-47-24.png

    Tho only references I have to the animator are:
    Code (CSharp):
    1. public Animator animator;
    2.  
    3.  
    4. animator = GetComponent<Animator>();
    5.  
    6. animator.SetBool("isIdle", true);
    7.  
    Why is this happening?
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You haven't shared any context around the code you shared, but I'd guess you're probably running
    Code (CSharp):
    1. animator = GetComponent<Animator>();
    in Start()? If you don't have an Animator on the same GameObject as this script, that code will set the Animator to null. Based on your screenshot, the Animator is on a different object (PlayerModel) so this seems to fit the bill. Since you've already assigned the Animator in the Inspector, there's actually no need to try to assign it again in code, so I would just remove that line of code if I were you.
     
  3. lucaskargreen

    lucaskargreen

    Joined:
    Sep 10, 2020
    Posts:
    21
    Yep that was it, Thanks alot!