Search Unity

instantiated object doesnt recognize ignore collision

Discussion in 'Physics' started by Gdakkutlu, May 5, 2019.

  1. Gdakkutlu

    Gdakkutlu

    Joined:
    Nov 9, 2016
    Posts:
    47
    Hey all,

    I've been trying to ignore collisions with specific elements in game.
    And I succeeded it but when a objects instantiates, it first doesnt ignore collision which it should.
    After first hit with other objects, it starts to ignore collisions.
    But it should always ignore it.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other)
    2.     {
    3.        
    4.         if(other.gameObject.tag == "redBall")
    5.         {
    6.             Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    7.             Physics2D.IgnoreCollision(gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    8.         }
    9.         else if(other.gameObject.tag == "greenBall")
    10.         {
    11.             Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    12.             Physics2D.IgnoreCollision(gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    13.         }
    14.         else if(other.gameObject.tag == "blueBall")
    15.         {
    16.             Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    17.             Physics2D.IgnoreCollision(gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    18.         }
    19.          else if(other.gameObject.tag == "blackBall")
    20.         {
    21.             Physics2D.IgnoreCollision(other.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    22.             Physics2D.IgnoreCollision(gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    23.         }
    24.  
    25.     }
    This is my ignore collision code. It works fine with other moving objects, but not the one just instantiated.

    I don't know why it happens.