Find closest object and return this

Hello, the problem i have is that when my AI needs to find the closest gameobject (a tree) i’m getting a huge frame drop, i know why, but can’t find a way to fix it.

At this moment when the tree spawns, it has a script (resource) based on the type its being added to a list with multiple tree’s.

To get a tree my AI runs a foreach loop trough the list and looks at which tree is the nearest with Vector3.Distance, the tree that is closest to the AI will be returned and set as destination, but the only moment its going trough the list it’s giving a huge frame drop, because the foreach loop loops trough an entire list of tree’s.

The question:

How can i find the closest tree in a clean way?

Thanks in advance,

why not add all the vectors of the trees to an array or a list and just check distance against the list?

If the ai can cut the trees down or whatever, consider making a class for the tree so that you can check if the tree still ‘exists’