Search Unity

Question Solved - Animator Missing Component Exception

Discussion in 'Getting Started' started by Deleted User, Mar 26, 2023.

  1. Deleted User

    Deleted User

    Guest

    MyTerrain has an Animator component.
    CollectWins uses a script with a public Animator which references MyTerrain.

    The CollectWins looks like this:

    Code (CSharp):
    1. public class EndGame : MonoBehaviour
    2. {
    3.     private int winCount;
    4.     private bool won;
    5.  
    6.     public Animator anim;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         winCount = 0;
    12.         won = false;
    13.         anim = GetComponent<Animator>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (!won)
    20.         {
    21.             if (winCount == 3)
    22.             {
    23.                 Debug.Log("YOU WIN! ALL GOALS ACHIEVED!");
    24.                 won = true;
    25.             }
    26.         }
    27.         else
    28.         {
    29.             anim.SetTrigger("RaiseTerrain");
    30.         }
    31.     }
    32.  
    33.         // below would be ++winCount functions (that work)
    34.    

    ===== image upload duration expired =====
    I'm sure this would have work as I expected. Nope. Any ideas?
     
    Last edited by a moderator: Apr 15, 2023
  2. Deleted User

    Deleted User

    Guest

    I thought the red boxes were a bit overkill... I've seen it done before so had a go.

    An update, I played my game and enabled the debugging inspector and found that Anim is NULL. When Start happens it isn't finding the terrain object.
     
    Last edited by a moderator: Apr 15, 2023
  3. Deleted User

    Deleted User

    Guest

    My mistake. I replaced the lines

    Code (CSharp):
    1. void Start()
    2.     {
    3.         winCount = 0;
    4.         won = false;
    5.         anim = GetComponent<Animator>();
    6.     }
    with
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         winCount = 0;
    4.         won = false;
    5.         Animator anim = GetComponent<Animator>();
    6.     }
    works now :)