Performance: Instantiating objects at runtime
This affects frame rendering speed and input responsiveness by adding workload during gameplay when new objects are created.
Use an object pool: for (int i = 0; i < 100; i++) { var enemy = objectPool.Get(); enemy.transform.position = spawnPoints[i].position; enemy.SetActive(true); }
for (int i = 0; i < 100; i++) { Instantiate(enemyPrefab, spawnPoints[i].position, Quaternion.identity); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct Instantiate in loop | Creates 100 new objects | Triggers multiple layout recalculations | High paint cost due to new objects | [X] Bad |
| Object Pool reuse | Reuses existing objects | Single layout update | Lower paint cost by reusing | [OK] Good |
GameObject clone = Instantiate(prefab, new Vector3(1, 2, 3), Quaternion.identity); Debug.Log(clone.transform.position);
GameObject clone = Instantiate(prefab, new Vector3(0, 0, 0)); clone.transform.rotation = Quaternion.identity;
Pass the test and this pattern moves unknown