I know that finding nearby objects that has colliders is super-easy using Physics.OverlapSphere. But how would I go about if I want to find nearby objects that doesn’t have a collider?
You’d have to loop through all the game objects and check their position.
To optimize, you’d have to use something like an octree, or otherwise code your own solution to keep track of what sector each object is in.
Collider[ ] hitColliders = Physics.OverlapSphere(center, radius);
This returns only colliders so you could then use hitColliders[0].gameObject. Remember OverlapSphere only returns colliders, so it limits your list to objects with colliders by default.
Derp…nevermind. I went and explained whta you already know.
wccrawford is right, you need to loop through all the gameobjects, and unless you only have a few you will want to optimize in some fashion. Once you do, GetComponent() will return null if non exists on the gameobject.
Just what I feared, but you mentioning Octrees means that I should trust you.
I’ve thought about that looping thing, where I loop through all the relevant objects based on their tag in Start() and store them. But I need to check this frequently and fear that looping through 1,000+ objects every second will be too expensive.
On the other hand, I really don’t want to start dealing with Octrees (or similar).
The problem here is that I have scenery with lots and lots of trees, and I don’t want thousands of them have their collider enabled unless when the players are close enough. I could have zone-triggered them, but given that a zone can have 100+ trees, or I will end up with lots and lots of zones, I was thinking there might be a better way.
Yes. The problem is finding objects that does NOT have a collider.