Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ignore collision not working

Discussion in 'Scripting' started by isanvel, Aug 7, 2019.

  1. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    41
    i am trying to make a gameobject ignore collision with other when it lands on the ground
    but that is not happening for some reason? am i doing something wrong?


    [SerializeField] GameObject boxGameObject;

    private void OnCollisionEnter2D(Collision2D ground)
    {
    if(ground.gameObject.tag == "Ground")
    {
    Physics2D.IgnoreCollision(boxGameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    }
    }
     
  2. Grizmu

    Grizmu

    Joined:
    Aug 27, 2013
    Posts:
    131
    You have the ground collider in Collision2D ground parameter, but you aren't using it.
    Currently lone GetComponents gets you the collider of the current gameObject to which the script is attached to.

    Try:
    Physics2D.IgnoreCollision(boxGameObject.GetComponent<Collider2D>(), ground.collider);