Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere

Hi

I am trying to make a more versatile way of entering vehicles I kinda am trying to “soft code” it instead of “hard coding” it like I did in the past.

var radius = 3.0;
function FindClosestVehicle(){
    
    var gameObjects : GameObject[] = Physics.OverlapSphere (transform.position,radius); // Error here
    
    for (var hit : GameObject in gameObjects) {
        if (!hit)
            continue;
        
        if (hit.gameObject){
            print(hit.gameObject.name);
           
    }
}
}

But is says “Cannot convert ‘UnityEngine.Collider’ to ‘UnityEngine.gameObject’” At the third line of code. I cannot see any collider information there unless OverlapSphere is only for collecting colliders. If so how would one go about finding gameObject using Physics.OverlapSphere?

Thank you my friend.

The physics system only works with colliders so [OverlapSphere][1] returns an array of colliders which bounds touches the sphere. An array of Colliders can’t be “casted” or “converted” into a GameObject array. You have to do it manually.

Colliders are components so they are always attached to a GameObject. To access this GameObject you can just use .gameObject on a collider.

Btw. in your for loop you have useless redundancy. The hit variable is of type GameObject so hit.gameObject is the same as hit.

However OverlapSphere doesn’t return a GameObject array so you should change your code like this:

var colliders : Collider[] = Physics.OverlapSphere (transform.position,radius);

for (var current : Collider in colliders)
{
    print(current.name);
    // To access the GameObject of that collider use:
    // current.gameObject
}

Oh and all Components attached to a GameObject share the gameobjects name, tag and layer so you don’t need to use current.gameObject.name
[1]: http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html

it’s all but elegant, but I think you could do something along the line of

var colliders : Collider[] = Physics.OverlapSphere (transform.position,radius);
var gameObjects : GameObject[];
for (var col : Collider in colliders)
{
  gameObjects.Add(col.gameObject);
}

[untested code!]

It should work, but there has to be a nicer way…?

Edit: Aw, Bunny was faster =3 … so there’s no other way after all o.o

Greetz, Ky.

A nice looking approach is to convert the whole array using a converter.

var radius = 3.0;
function FindClosestVehicle(){

    var colliders = Physics.OverlapSphere (transform.position,radius);
    var gameObjects = System.Array.ConvertAll.<Collider , GameObject>( colliders , function(x) x.gameObject );

    for (var hit : GameObject in gameObjects) {
        if (!hit)
            continue;

        if (hit.gameObject){
            print(hit.gameObject.name);

    }
}
}

The nice thing about that is you don’t have to rewrite your enumerator since you will have a gameObject array. there will be no need to change the enumerator to change the collider to a game object in the loop.

Note: I don’t know if you can use generic methods in js. I know generic types work, but I’ve never tried generic methods so I’m writing it in C# too

float radius = 3.0f;
public void FindClosestVehicle(){

    var colliders = Physics.OverlapSphere (transform.position,radius);
    var gameObjects = System.Array.ConvertAll<Collider , GameObject>( colliders , x => x.gameObject );

    for (var hit  in gameObjects) {
        if (hit == null)
            continue;

        else {
            print(hit.name);

    }
}
}