Question about Vector creation
If this has been asked before I'm sorry, I couldn't figure out what to type in google to find the answer!
I have the option of either creating a named Vector at the top of my code class with something like:
Vector3 myVector = new Vector3( 0, 0, 4.0f);
...and then using/updating this vector to a different value as needed in a method such as the update method like;
void Update ()
{
transform.position += myVector;
}
or I could not create that named vector at the top and inside one of my methods I could do something like this:
void Update ()
{
transform.position += new Vector3(normalSpeed, 0, 0);
}
...which seemingly creates like a "throwaway" vector that's not named and presumably discarded.
I'm interested to know; what are the considerations/differences? Should I always use the named Vector method where possible to save memory? If so, why do a lot of tutorials use the "throwaway" method?
Answer by migueeel · Jun 14, 2017 at 12:05 AM
My wild guess is that if you're going to use the vector only once, do the throwaway version as you don't need to store a variable that you'll use once.