Getting all GO's within a radius of the camera...

Hey guys,
How would I go about filling an array with GO's (or names) that are within a certain radius of the camera/player?

Also while I'm at it, if I have a script on each of many many objects but do not do anything in any update function in them (only do something when asked, and only on the few near the player at a time) would there still be a performance hit with all those scripts?

Thanks guys,
-Jeremy


The easiest way is to use Physics.OverlapSphere. But that works only against colliders of course. Otherwise you can do, FindObjectsOfType(Transform) and compare the distance against every one of them.

[quote]
Also while I'm at it, if I have a script on each of many many objects but do not do anything in any update function in them (only do something when asked, and only on the few near the player at a time) would there still be a performance hit with all those scripts?
[/quote]
Yes it does cost performance. There is a good way to avoid it though.

enabled = false;

This will make the script update and fixedupdate function not execute.
A script that is marked as disabled doesn't consume any processing power.

Great, thanks Joe.
-Jeremy

Ok, with the Physics.OverlapSphere, it returns an array of the colliders. How do I get the gameobjects that the colliders are attached to? It isn't as easy as typecasting is it? I am a bit dense tonight so I apologize if this sounds stupid. :)

-Jeremy

Any component has a property gameObject and the same properties you use to access other components from your scripts normally. eg. transform or rigidbody.

var colliders = Physics.OverlapSphere(Vector3.zero, 10);
for (var c : Collider in colliders)
{
   print(c.gameObject.tag);
   c.transform.position = Vector3.zero;
   c.rigidbody.AddForce(Vector3.up);
}

Oh great. I was thinking it would be hard with parents and children and such...

Thanks a lot Joe,
-Jeremy