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 Checking an Animator bool when colliding with it

Discussion in 'Scripting' started by thewonderingvagabond, Apr 23, 2023.

  1. thewonderingvagabond

    thewonderingvagabond

    Joined:
    Mar 11, 2023
    Posts:
    16
    Hi,

    I am trying to figure out whether the animator parameter has been set to true when the player collides with an object. I have the following code on a script with the player. However, this is checking the animator for the player. How can I check the collided objects's parameters?

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.        
    4.         if(anim.GetBool(Tags.STITCH_ANIMATION))
    5.         {
    6.             return;
    7.         }
    8.         else
    9.         {
    10.             totalstitches++;
    11.         }
    12.        
    13.     }
    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    From the
    collision
    variable you can get the Collider, and from that you can use GetComponent<T>() to retrieve the animator on that GameObject.

    BEWARE that it may not be present on that GameObject in all cases, so it is up to you to guard the usage of anything you get back from GetComponent<T>() to ensure it is not null.
     
  3. thewonderingvagabond

    thewonderingvagabond

    Joined:
    Mar 11, 2023
    Posts:
    16
    So easy ;)
    if(collision.GetComponent<Animator>().GetBool(Tags.STITCH_ANIMATION))

    Thanks
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Be sure you read the second part of my post too or else your game will crash and you'll need this post:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    You are hereby warned twice now.
     
  5. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685

    Just for my own clarification, checking for the presence could be done like this?

    Code (CSharp):
    1. if(collision.GetComponent<Animator>()) {
    2.      // exists, now check the bool
    3. }
    4. else {
    5.     // maybe do something else if no Animator component, maybe do nothing
    6. }
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Yes, I would just use a local variable though

    Code (csharp):
    1. var animator = GetComponent<Animator>();
    2. if (animator)
    3. {
    4.    // go to town with your properties
    5. }
    EDIT: for the C# enthusiasts out there, AFAIK you may NOT use the null coalescing operator in lieu of the above if statement because Component derives from UnityEngine.Object and hence has that bool override.
     
    Last edited: Apr 25, 2023
    seejayjames likes this.