Search code examples
c#unity-game-enginegameobject

GameObject.name vs new GameObject(name)


Is there a difference performance-wise between using

Example 1

var myObject = new GameObject();
myObject.name = "myObjectName";

And

Example 2

var myObject = new GameObject("myObjectName");

Some articles like this suggest avoiding Example 1 because native code is called, hurting performance.

Out of curiosity, I checked the documentation of the GameObject class and there are no remarks indicating how any of the constructors might affect performance.

My current theory is that Example 2 is similar to instantiating an object and immediately specifying a parent as that is supposedly better than instantiating an object and specifying the parent later.


Solution

  • I have answered my own question.

    The second example runs 0.41 milliseconds faster, but allocates the same amount of memory as the first.

    enter image description here