Alox Alox -4 years ago 234
C# Question

Simplify foreach on Transform

Is it possible to simplify the following code by using Linq?

Transform parent;
List<GameObject> children = new List<GameObject>();
foreach (Transform child in parent)

I was hoping to simply write parent.ForEach() but transforms don't have such convenience. Does anyone know of a simplification for this?

This is only curiosity and not necessary.

EDIT: parent is not a list of children but a transform.

Attempt at Bal√°zs' answer

Answer Source

If Transform implements IEnumerable, but not IEnumerable<Transform> then you first need to convert it to the generic version before you can use Linq:

List<GameObject> children = parent
                               .Select(t => t.gameObject)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download