I am trying to avoid interactions between instantiated bullet and the player colliders.tho the script works, there seems to be some sort of delay

{ public GameObject pTemp;

// Start is called before the first frame update
void Start()
{
    pTemp=GameObject.Find("Player");
    

}

// Update is called once per frame
void Update()
 {  // hscript=handAcc.GetComponent<handMove>();
    
}
void OnCollisionEnter2D(Collision2D other){

if(pTemp.GetInstanceID()==other.gameObject.GetInstanceID()){
	
	Physics2D.IgnoreCollision(GetComponent<Collider2D>(), pTemp.GetComponent<Collider2D>());
}

}
}
//script attached to bullet prefab

You need your bullet to ignore collision with player? Easy, just make a new layer for bullets, assuming you already have one for player.
Next go to Edit => Project Settings => Physics or Physics2d in your case, and uncheck the crossing checkbox of layers Player and Bullet.

You need a more elegant layer detection on the bullet or better define the layers, but here is a solution that can help you out while you figure out what route you want to go.
You can setup a trigger collider that extends towards right outside of where your bullet is instantiated, then at that point do an on trigger exit and set a bool to true.
I’ll give you a step by step of what I’m talking about:

  1. Create a collider (any will do) and set it to trigger, then set the bounds so they are RIGHT outside of the bullet instantiation.

  2. On the script that causes damage to the bullet, create a bool and set that bool to false by default.

  3. On that same script refrenced above, type in this line:

    OnTriggerExit2D(Collision2D col)
    {
    if col = gameObject;
    {
    Damage = true;
    }
    }
    

    And set up the damage part of your bullet prefab to make sure that the bool located on it is set to true.

That solution is a band aid solution until you find something that would better define your player vs enemy layers or a way to better define that stuff from the bullet.

Make sure the collider for your bullet is checked as “is trigger” that way it won’t really interact with the player, it does collide but it won’t cause any problems

Hi there,

Why would you want to “IgnoreCollision”, once the collision has occur?

You probably may want to do it in the Start method, so once the bullet is instantiated, is going to be created ignoring the collision.

So your Code don’t need OnCollisionEnter2D, and will be:

void Start()
 {
     pTemp=GameObject.Find("Player");
     Physics2D.IgnoreCollision(GetComponent<Collider2D>(), pTemp.GetComponent<Collider2D>());     
 }

Hope it helps.