How can I reach collisions of subobjects of prefab

I have a composite prefab that consist of multiple objects (sprites with collisions etc). I need to create a list of all collisions at some point, I have this script that partially works:

    // this works! - we add all collisions that are on "top level" object
    List<Collider2D> list = new List<Collider2D>(this.owner.GetComponents<Collider2D>());
    // this doesn't
    foreach (Transform sub in this.owner.GetComponents<Transform>())
    {
        foreach (Collider2D c in sub.GetComponents<Collider2D>())
        {
            if (!list.Contains(c))
                list.Add(c);
        }
    }
    foreach (Collider2D collision in list)
        Physics2D.IgnoreCollision(this.collider2D, collision);

My problem is that I have a player that is made of multiple parts, like legs etc, now when I shoot from a weapon, bullet ignores all collisions of player who shot it (thanks to this script) but it doesn’t ignore collisions of these extra parts like hands or legs, so at certain angle, the player can shoot themselves.

Try using GetComponentsInChildren()

Instead of the line where you get the transforms.
As a bonus, you should be able to get rid of a for loop!

Also, of you put the player on its own layer, you can use the edit->physics menu to make it so the player doesn’t collide with its own layer, which removes the need for this script entirely.