Search Unity

ignore collision not working

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

  1. isanvel

    isanvel

    Joined:
    Mar 21, 2019
    Posts:
    15
    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. Griz

    Griz

    Joined:
    Aug 27, 2013
    Posts:
    115
    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);