If you can assign the playerCar
via the inspector and not having to
search for it and finding it in your
scene, that would be great!
(performance-wise)
if (
Vector3.Distance(playerCar.transform.position,
transform.position) < 20)
Two things I don’t like about this:
Don’t use hard-coded values, store
20 in a variable instead, and then
use it in your code.
If you wanna compare the distance
between two points, it’s better to
compare the squares of the distances
instead of the real distance which
involves a square root operation,
which is a bit expensive.
if ((pos1 - pos2).sqrMagnitude <
myDist * myDist)
// do something
Disable/Enable your mesh renderer
instead of Destroying and
re-instantiating it.
Lastly, if you’re not using anything
from System.Collection namespace,
remove it.
Based on the information you’ve given you have two options to manage your memory.
Reuse your gameobjects (instead of removing components and adding them, move whole child game objects) so if you have a collection of maximum trees that can be in view move them to a pool when they go out of view and from there into the correct location as it comes into view.
Use sub scenes and load / unload them…this of course depends on the size of the levels as to if this would work for you.
I think they are your best options but it really depends on the scope of your game.