In C# there are two kinds of types, roughly: value types and reference types.
You use new
when you create both, but value types are created on the stack (most of the time), and only reference types get created on the heap. Once created, reference types stick around until the garbage collector happens to come along, determine they are no longer needed, and collects them. Instances on the stack are destroy efficiently when the stack frame goes away (when the function they were created in ends).
Vector3
is a value type in Unity, so almost all the instances you ever create will be stored on the stack and thus cheap to both create and destroy. So you're not likely doing anything wrong here. It certainly doesn't create a memory leak, and it almost certainly won't be a performance issue (you'd want to profile to be sure, anyhow).
Creating a lot of new reference types every frame can be problematic since that can induce the garbage collector to run more frequently, causing hitches as it pauses all your threads to do its work. But value types like Vector3
are a pretty safe bet.
new Vector3
becauseVector3
is astruct
. If it is a class then that's a problem. Read the whole post about using Object pooling. \$\endgroup\$