Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question How to use one script to run two animation with OnCollisionEnter2D

Discussion in 'Scripting' started by kamivalk, May 10, 2021.

  1. kamivalk

    kamivalk

    Joined:
    Dec 5, 2020
    Posts:
    6
    i want to make a player collide with two buttons to open a door , i want to use same script to run t


    Code (CSharp):
    1. public class RedButton : MonoBehaviour
    2. {
    3.  
    4.    
    5.     private Animator anim;
    6.     void Start()
    7.     {
    8.         anim = gameObject.GetComponentInChildren<Animator>();
    9.     }
    10.  
    11.     private void OnCollisionEnter2D(Collision2D collision)
    12.     {
    13.         if (collision.gameObject.CompareTag ("Player"))
    14.         {
    15.             anim.Play("PushButtonOne");
    16.         }
    17.         else if (collision.gameObject.CompareTag("Player"))
    18.         {
    19.             //second button animation
    20.         }
    21.     }
    22.     private void OnCollisionExit2D(Collision2D collision )
    23.     {
    24.         if (collision.gameObject.CompareTag("Player"))
    25.         {
    26.             anim.Play("IdelOne");
    27.         }
    28.  
    29.         else if (collision.gameObject.CompareTag("Player"))
    30.         {
    31.             //second button animation
    32.         }
    33.     }
    34. }
     
  2. kamivalk

    kamivalk

    Joined:
    Dec 5, 2020
    Posts:
    6
    i found a solution after 6 hours of searching thanks guys :)