- Home /
Question about "new" keyword and performance
I've read that we should avoid allocating memory at runtime. Does this apply to every type of variable? I know that I shouldn't use new to initialize reference class object at runtime. But what's about struct like Vector2 or Vector3? Should I avoid to create (using new) them at runtime too?
Answer by loopyllama · Mar 26, 2011 at 09:34 PM
anytime you want to set any component of a transform you have to call new Vector3, directly in c sharp and under the hood in javascript. to quote trinity from the matrix, "Dodge this!"
What I don't understand is that Vector3 is a value type. Its properties are float. What's so much different between create new Vector3 and create three new float variables?
there isn't too much difference. under the hood, floats are structs in .net. my point is that you can't avoid new Vector3 at runtime, so why worry? what kinds of trouble are you running into that causes you to ask your question? There are many knowledgeable people in the community that can quickly answer a very specific question.
So, you basically mean that I shouldn't worry about it right? I also use new Vector3 but the use of new keyword makes me curious if it's appropriate or not. Because I've read that it's a bad thing to frequency create new object at runtime. Thanks for the answer. :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Total memory allocated increases indefinately from .80GB untill crash 3 Answers
Efficient AutoFire Burst Script Help 2 Answers
What are the benefits of using Invoke over Coroutine or vice versa? 1 Answer
FixedUpdate performance doubts 2 Answers