I’ve been googling around to find a script that finds the nearest object but I havent been sucessful getting any to work. They are all in JS and i want in C# =\
How can I find the nearest object to the object that is running this script that has the tag “EnergyLink”?
Well,
You could create an array of GameObjects that you want to test for distance.
Then periodically check the distance between it and the player.
You can check the distance with Vector3.Distance:
If you need further assistance then feel free to ask.
(This may not be the most optimal way but for me personally it makes sense, feel free to correct me.)
Another way you could do it is with OverlapSphere. Start with a small radius and increase it if you don’t find anything. The first thing you hit is your winner. OR use a constant radius as a range, loop through the hit colliders looking for the tag, then compare distances with Vector3.Distance.
If there aren’t very many objects with that tag in the scene, do as above posted and simply keep those objects in an array. loop through and compare distances as needed.
And if all else fails there is always FindGameObjectsWithTag. That really just looks through every single gameObject and returns an array of all GameObjects with the tag you want. The least elegant of the solutions, but it gets the job done.