- Home /
Question by
Matthew A · Jan 30, 2011 at 02:49 PM ·
gameobjectcolliderarray
get Collider[] as GameObject[]
I want to get the GameObjects in a sphere, as an array, rather than the colliders. I can do this manually (iterate through and use .gameObject), but is there a more concise way of converting the whole array? Something like:
GameObject[] gos = Physics.OverlapSphere(/* ... */) as GameObject[]
Comment
Best Answer
Answer by Mike 3 · Jan 30, 2011 at 03:36 PM
You can use Array.ConvertAll to do it in one relatively simple step:
GameObject[] gos = System.Array.ConvertAll(Physics.OverlapSphere(Vector3.zero, 10), col => col.gameObject);
Uses a lambda function to grab the gameobject each collider is connected to