I need help with using LINQ to find specific collider

I have two circle colliders on a gameObject, one is a trigger collider and the other is not. I’m trying to cache a reference to the trigger collider using LINQ.

I am able to do this without LINQ using:

CircleCollider2D[] _Colliders = GetComponents<CircleCollider2D>();
List<CircleCollider2D> _triggerColliders = new List<CircleCollider2D> ();
foreach(CircleCollider2D _col in _Colliders){
			if (_col.isTrigger) {
				_triggerColliders.Add (_col);
			}
		}
CircleCollider2D _myTriggerCollider = _triggerColliders [0];

I’m trying to do this simple thing using LINQ instead.

When I try

	CircleCollider2D[] _Colliders = GetComponents<CircleCollider2D>();
	CircleCollider2D[] _triggerColliders = _Colliders.Where (x => x.isTrigger);

I get an error indicating that I have to perform an explicit cast… so I try

CircleCollider2D[] _Colliders = GetComponents<CircleCollider2D>();
CircleCollider2D[] _triggerColliders = _Colliders.Where (x => x.isTrigger) as CircleCollider2D[];
CircleCollider2D _myTriggerCollider = _triggerColliders [0];

And I get a null reference error.

What am I doing wrong?

Looks like the problem was using “as CircleCollider2D” to convert to an array. I’ve since learned that you have to use extension methods like .toArray() or .toList() to accomplish this.

Sure would have been nice to get a message indicating this…