I read somewhere that I shouldn't create any new object instance in the Update() method of the game engine, because this will lead to wake Garbage Collector up and decrease performance, but sometimes I see in some tutorials that they use new keyword in unity Update() method! Is this okay? And unity will handle this in someway, or no?
-
1it depends on what you instantiate how often. also the GC has more to do with destroying gameobjects (where the answer is the same, what and how often is key). if you need to repeatedly instantiate and destroy similar objects look into pooling, they even have a (simple) tutorial on their official page https://unity3d.com/de/learn/tutorials/topics/scripting/object-pooling – yes Jul 20 '17 at 22:26
-
2This is also a great article for managing garbage [Optimizing garbage collection in Unity](https://unity3d.com/learn/tutorials/topics/performance-optimization/optimizing-garbage-collection-unity-games). General rule of thumb is indeed to reduce the amount of New objects created in frequent methods but that's not to say it is forbidden. – ryeMoss Jul 20 '17 at 22:30
-
@ryemoss I should read this article carefully, seems to be a nice article, thanks for showed me the way:) – MehDi Jul 20 '17 at 22:58
-
@yes This wasn't completely what I need to know, but thank you very much to give me a good idea! Is this concept same as thread pools?I'm not so familiar – MehDi Jul 20 '17 at 23:02
1 Answers
This is a confusing thing for new C# programmers.
Is this okay?
The answer to this depends on the datatype that you using the new
keyword on. This is basically value type vs reference type. See the link at the end of this answer for more information on this.
- If it is a
class
, then don't use thenew
keyword on it in theUpdate
function. Use Object Pooling or you will be creating new Object every frame. Yes, this is costly. - If it is a
struct
, you can freely use thenew
keyword on it in theUpdate
function without.
You may have seen a code like this in the Update
function:
Vector3 vec = new Vector3(0, 0, 0);
That's fine because Unity's Vector3
is a struct
not a class
.
Another example is this in the Update
function:
GameObject obj = new GameObject("MyObject");
This is bad because GameObject
is a class
. You are creating a GameObject each frame. Do this once in the Start
function then re-use it. You can also use object pooling.
Notice the circled text in red color, you can use that to determine if the API you are using is a class
or a struct
.
Here are other things to know:
1.C# value type vs reference type
2.is there a point in recycling value types unity
3.What's the difference between struct and class in .NET?
Note that this answer focuses on using the new
keyword in Unity as mentioned in your question. There are still other things that can allocate memory without even using the new
keyword. Examples includes using the param
keyword in a function parameter, returning an array in a function, using yield return 0
instead of yield return null
.
And unity will handle this in someway, or no?
The only one that can answer this question is the device you are running this code on. Usually, you get some hiccups on mobile devices due to GC running. Object Pooling is very important on mobile devices.

- 121,791
- 22
- 236
- 328
-
Perfect answer! Exactly what I needed! Thank you very much friend:) Please give me more advice about the way which I should go to be an advance programmer like you that know such deep details, but of course if it doesn't take your time:) – MehDi Jul 20 '17 at 23:11
-
You are welcome. I am not an advanced programmer yet, I am still working to get there. My advice to you is to: **1**.Read other peoples code. You can do this by finding an opensource project and trying to understand and improve/contribute to it .You will learn a-lot than you will ever learn from school by doing this. **2**.Program every day(at least 5 hours). **3**.Become friends with those who program. **4**.Go to conferences, listen to their speech and ask questions on how they accomplished some certain features. – Programmer Jul 20 '17 at 23:30
-
-
I assume you ask this for Unity so learn something very week. Learn things that really matter. Make a timetable to understand coroutines, generating terrains/meshes during run-time, shaders(I am still learning this), path findings, compute shaders, networking. This covers 80% of what you need to know to actually make a complete game in Unity. See [this](https://unity3d.com/learn/tutorials/s/scripting) link for beginners. Happy coding! – Programmer Jul 20 '17 at 23:35