- Home /
Convert GameObject list to Transform list
I want to get a list of GameObject.FindGameObjectsWithTag("FallingObject") and convert it to a list of all the objects' transforms, but hopefully with one line of code. Is this possible to do with C# lambda expressions? I've been using foreach loops to pull transforms out, and I'd rather not do all that juggling.
Teach me some Unity-relevant tricks please. I'd love to learn. Lambda has always been a little counter-intuitive to me.
If you have a list of GameObjects, you already have the list of Transforms via gameObject.transform - ?
Answer by JustinReinhart · Jan 23, 2015 at 04:24 AM
Got lucky with Google and answered my own question. Step below actually goes one step further and collects the Vector3 position of each Object.Transform and returns it to a List.
PositionsToTrack.AddRange( GameObject.FindGameObjectsWithTag("FallingObject").Select( item => item.transform.position ) );