Performance: Creating and naming GameObjects
This affects the initial load time and runtime performance by influencing how many objects the engine must manage and how efficiently it can find and update them.
for (int i = 0; i < 1000; i++) { GameObject obj = new GameObject($"Enemy_{i}"); }
for (int i = 0; i < 1000; i++) { GameObject obj = new GameObject(); // No name assigned }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unnamed GameObjects | 1000 new nodes | N/A | N/A | [X] Bad |
| Named GameObjects | 1000 new nodes | N/A | N/A | [OK] Good |
GameObject player = new GameObject("Hero");new GameObject("Hero") which creates a new GameObject with the name "Hero".player for later use."Enemy" is correct.enemy using the = operator, which is correct.GameObject obj = new GameObject("Tree");
Debug.Log(obj.name);new GameObject("Tree") sets the GameObject's name property to "Tree".Debug.Log(obj.name) outputs the name of the GameObject, which is "Tree".GameObject enemy = new GameObject(Enemy); enemy.name = "Orc";
Enemy is missing quotes, so it is treated as a variable, causing an error.enemy.name = "Orc"; is allowed and correct after creation.Pass the test and this pattern moves unknown