Performance: Instantiating objects at runtime
MEDIUM IMPACT
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 |