Ignore collision problem.

Hi. I want to tell a gameobject to ignore collision with all gameobjects which has “Enemy” tag. But I get this error: BCE0019: ‘collider2D’ is not a member of ‘UnityEngine.GameObject’.

Script:

Physics2D.IgnoreCollision(this.gameObject.collider2D,GameObject.FindGameObjectsWithTag(“Enemy”).collider2D, true);

GameObject.FindGameObjectsWithTag(“Enemy”) returns an array of Gameobjects. You need to iterate trough it to get collider2d of gameobject.

GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");

for (int i =0, lenght = enemies.lenght; i < lenght ;i++)
{
Physics2D.IgnoreCollision(this.gameObject.collider2D, enemies*.collider2d )*

}
above is pseudocode

There’s more than one problem with what you’ve done there.

First you’re calling GameObject.FindGameObjectsWithTag(“Enemy”) which returns a LIST of gameobjects, and then you’re asking for its collider2D, which a LIST doesn’t have. You want the collider2D from each member of that list. That’s the error you got there.

Physics2D.IgnoreCollision only takes Collider2D as parameter, and there you’re passing a LIST, which will give you an error after you solved the first problem.

Solution:

foreach(GameObject obj in GameObject.FindGameObjectsWithTag("Enemy")){
   Physics2D.IgnoreCollision(collider2D, obj.collider2D);
}

On a side note, if you’re ignoring more than one object you should think about putting those objects in a specific layer and then just use the function Physics2D.IgnoreLayerCollision