IgnoreCollision with multiple colliders

I’ve been having trouble getting IgnoreCollision to work. I have a gameobject called Barrier that is basically 4 boxColliders and a ship prefab with 3 boxcolliders. The following code happens in the ship initializing. I want the ship to be able to fly through the barrier. I’ve tried a lot of different things but so far nothing has worked. There has to be something I’m overlooking. Any ideas what I’m doing wrong?

	var shipColliders = GetComponents(Collider);
	wall = GameObject.Find("Barrier");

	var wallColliders = wall.GetComponents(Collider);
		
	for (var shipcol : Collider in shipColliders) 
	{
		for (var wallcol : Collider in wallColliders) 
			{
				Physics.IgnoreCollision(shipcol.collider , wallcol.collider);
			}
	}

GetComponentsInChildren(Collider);

:slight_smile:

That’s not working either.

Also, what type should wallColliders be? When trying to explicitly typecast it I get an InvalidCastException error trying something like this…

var wallcolliders : Collider[];

GCIC returns an Array.

colliders1 : Array = player.GetComponentsInChildren(Collider);
colliders2 : Array = barrier.GetComponentsInChildren(Collider);
for ( var c : Collider in colliders1 ) {
  for ( var c2: Collider in colliders2 ) {
    Physics.IgnoreCollision( c1, c2 );
  }
}

That’s exactly how I get all the random stuff to not interact with my player, and it works for me.
Perhaps your Find operation isn’t returning the right object?

I know GameObject.Find(“Barrier”) is working. I was explicitly typecasting everything so I could check the values in the editor. That one I know was working. I’m not sure about the ship though. And I’m not sure what GetComponents is actually getting because I can’t set it up to check in the editor. Either way, when it does run with no errors, my ships are still colliding with an invisible wall.

I’ll try something closer to what you suggested when I get home tonight. Thanks for the help.

You could try

var arr : Array = GetComponentsInChildren(Collider);

for ( var x in arr ) {
  Debug.Log(x.name + " is a Collider child of " + gameObject.name);
}

Or somesuch to make sure you’re pulling from the right place.

Ok, I still can’t get it to work.

Here’s what my code looks like now…

var barrier = GameObject.Find("Barrier");

var colliders1 : Array = GetComponentsInChildren(Collider);
var colliders2 : Array = barrier.GetComponentsInChildren(Collider);
for ( var c1 : Collider in colliders1 ) {
  for ( var c2: Collider in colliders2 ) {
    Physics.IgnoreCollision( c1, c2 );
	Debug.Log(c1.name + " is ignoring " + c2.name);
  }
}

The log shows all the collisions that I’m expecting. The other log you suggested looks like it’s getting everything it’s supposed to. There has to be something simple that I’m overlooking.

Thanks for the Debug.Log suggestion by the way. That’s helping out a lot.

Finally got it working when I added it after the instantiate function. So I just shoved everything in a newShip() function. I still don’t know why it doesn’t work from a script attached to the ship though. Thanks again for the help.