- Home /
Should I delete a Vector created with new operator?
I'm quite new to C# and Unity so please, be tolerative, it's probably a very obvious question :D
When I create a Vector (Vector2 in this case) like that:
Vector2 Position = new Vector2(0, 0);
should I delete it when it's not used to prevent the memory leak? And if so, how can I do that? I can't use Destroy() for sure.
Answer by Kossuranta · Dec 19, 2016 at 09:45 AM
You don't have to mind about that when using language like C# which has garbage collection. This is something you would need to think with lower level languages like C or C++.
Just to add: Unity's vectors are structs. They themselves are not allocated by the garbage collector and are therefore not garbage collected.
Structs (as all value types) are allocated on the stack. The memory on the stack is reclaimed when the stack pointer is popped (returned from a method).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Deleting an Element from an Array. 1 Answer
Making the camera follow the mouse but stay near the player? 2 Answers
I have a problem with coins 1 Answer