# Most optimal way to find closest object

So currently ive been using the physics.castSphere method with a small radius to find the closest of an object with a tag.

However this is pretty performance heavy so my question is, is there a way to optimize it?

i have also been looking at keeping a list of gameobjects and just looping through those however doing that a few times a frame for each object searching also seems rather performance intensive.

Here is my case:

50 individual characters on team 1

50 individual characters on team 2

each character has to find the closest of the character from the other team.

What is the fastest and least performance heavy way of doing it?

• Have you actually profiled? Looping over 100 objects a few times is nothing Aug 28, 2019 at 12:45
• @ratchetfreak so you are suggesting that i keep going with the "keep a list in memory" approach? This approach can get messy when objects are spawned / despawned Aug 28, 2019 at 12:46
• @ratchetfreak however it might be the fastest way Aug 28, 2019 at 12:47
• There's no method called physics.castSphere — do you mean Physics.SphereCast, which fires a sphere along a ray (significantly heavier than you want) or Physics.OverlapSphere, which finds objects in a spherical radius (and has a non-allocating version you can use)? Do you only care about the closest object within a particular "awareness" radius, or do you still need the closest object even when it is very far away? Aug 28, 2019 at 13:05

if calculations are heavy you can divide them using something like Coroutine to avoid lags and...

if count of object that you need to calculate their distance is low you can simply calculate their distance or use rayCast if its important that nothing is between them.

if they are too many you can put a circle or... collider around it that only calculate those are in the range. you can make trigger bigger if there is no object in range.

if you use trigger you have to use layer masks to only calculate range for those only important.

• It's worth noting that Coroutines are not meant to be used to avoid lag on something that is meant to run every frame. Coroutines are meant to run code without blocking the main thread. Depending on what one is working on, they can also make it harder to access objects. Aug 28, 2019 at 13:16
• @TomTsagk as i know unity has not multiple thread everything is in one thread. coroutines only calculate frame timing to do jobs in some amount of time. sometimes in your games you need to use for loops. its a timing process. coroutine is the main solution for loops and ... on game main Update thread Aug 28, 2019 at 13:25
• Yes but if you want to run something on every frame, and that something takes a long time to compute, I don't think Coroutines will improve that at all. My point was, if someone is concerned about improving performance, Coroutines are not the right way to go. Aug 28, 2019 at 13:29
• @TomTsagk calculating distance in every frame is a waste. certainly. even if it always is being done, it should be done in every 4 or 5 seconds not in every frame. Aug 28, 2019 at 13:44
• I really disagree with that. Can you image a particle system where the particles can only collide with each other every 4 or 5 seconds? Plus this is getting a little off-topic, I just wanted to point out that Coroutines are not some special functions that are immune to performance issues. Aug 28, 2019 at 13:53