Physics2D.IgnoreCollision.... with an array of parameters

hi, I am not sure how to code this please help…

 public Collider2D PCol;
    public Collider2D[] BCol;

void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Enemybullet")
            {
                Physics2D.IgnoreCollision(BCol, PCol, true);
                StartCoroutine(Bullet());
                health -= 1;
            }
}
IEnumerator Bullet()
    {
        yield return new WaitForSeconds(2f);
        Physics2D.IgnoreCollision(BCol, PCol, false);
    }

the problem is that in the line:

Physics2D.IgnoreCollision(BCol, PCol, false); and
Physics2D.IgnoreCollision(BCol, PCol, true);

the “Bcol” is not accepted…

Please help me to state it correctly…

There is no overload for the IgnoreCollision method that takes an array of Colliders. Instead, use a loop that iterates your array and call IngoreCollision for each of them (skip nullvalues).

What if I change it to GameObject and in Void start just …GetComponent…?

That would work, but using GetComponent (singular) will only return a single Collider, whereas your snippet deals with an array of Colliders. Of course, there’s also GetComponents (plural) and similar methods that return arrays of components, but you’ll end up having the same situation: you need to iterate the array that you want to register.

Check the documentation for Physics2D.IgnoreCollision.

Unless you only want to deal with a single collider (which can be achieved by removing the square brackets in your snippet), you will need something like

foreach (var collider in BCol)
{
    Physics2D.IgnoreCollision(collider, PCol, true);
}

That’s it.

You can also use Layers to define which objects shall be able to physically react with each other, which could be another option if you have to do this frequently.

my enemies shoot a row of bullets, and with each collider I lose a life… that means that when the row of bullets hits my player, he looses all 3 lives in stead of just one…
so i was trying to make an array of all the bullets…

but let me try the code you suggested…

Oh, I see.
Well, then my solution is not the one you’re looking for, as that would simply register them all at once.

But that’s a thing you can achieve in many ways. Destroy them, or deactivate them, or change their layer, or keep track of the bullets yourself and prevent them from damaging your player multiple times.

Thanks for the help, I have tried a number of methods and am not sure how I’m going to do this…

Okay, so one way is to destroy/deactivate them. When a bullet hits something, simply destroy/deactivate it.
Downside: The bullet is “gone”, it won’t be there for visualization unless you disable or remove its collider, but then, it won’t react to any physics anymore.

Next option, you can change their Layer. A layer (top of the object in the inspector) can be used to configure physics interactability. For instance, you can say that LayerA shoul completely ignore LayerB objects (basically a less granular thing to your approach, which attempted to register individual pairs of colliders).
Anyway, you can use layers and move the objects to a different layer, for instance “IgnoreDamagables” layer.
Downside: If you have piercing bullets, this will turn out to be inconvenient and leads to a mess.

So, at this point, your approach is still a better one.
Using your approach, you would not pass the array to the method, but any bullet’s collider that enters the trigger. It’s a tiny change in your code. Instead of passing the array, you should be able to do

Physics2D.IgnoreCollision(collider, PCol, true);

(Note: I renamed collision paramter to collider as it was part of your snippet. This can be confusing as there is also a Collision class, which is used during OnCollisionXYZ, whereas OnTriggerXYZ uses Colliders.

This line of code does not require pre-known bullets (as in your public and serialized BCol array). With the other lines of code in your snippet, it’d filter bullets by their tag, and then ignore the bullets collider, which comes in as the argument.

I’m not entirely sure how much overhead this generates. Thinking about it, the registration should simply prevent the OnTrigger message from being sent to the objects if the registered collider mappings happen to define a physical interaction, though that’s not obvious.

However, you can also roll your own small tracking system. For instance, keep track of whether something has been hit or not.

is it possible to increase to increase a collider, when the prefab is increasing???

What do you mean?

Btw, I just found a mistake in my post and corrected it.

I

I mean is it possible to make a prefab with different bullets as children and have the collider increase in size while the bullets move further away from each other…
bullet 1 = y axis 1, x axis 10
bullet 2 = y axis 0, x axis 10
bullet 3 = y axis -1, x axis 10

so that means the bullets will spray… I was thinking of connect a collider to the positions of bullet 1 and bullet 3… so that means that only 1 collider will connect with the player…so with every frame the bullets go further and further apart, and the collider grows bigger…

Well if I could do something like this… how?

Thanks for the correction…

Well yes, that would be possible. If you think about it, you’d have collision detection between them, too. False positives when it comes to damaging the player or anything else.

If you need actual physics bullets, use a collider per bullet. Or use raycasting…

but if I don’t connect a collider to the bullets themselves, just the Prefab parent? then when the bullet is instanciated only one collider will exist for all the bullets?but would that transform the points of the collider and how would I do that?

It would not automatically resize the collider. You need to do this programmatically.

Thanks, I figured… not sure how to code it… i am having problems getcomponentsize.x,size.y;

I used
item = TheItem.GetComponent()size.x;

previously to reference it, but not to change it…