Performance: GetComponent usage
MEDIUM IMPACT
This affects frame rendering speed and input responsiveness by how often and when components are accessed in Unity.
private Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
}
void Update() {
rb.AddForce(Vector3.up);
}void Update() {
var rb = GetComponent<Rigidbody>();
rb.AddForce(Vector3.up);
}| Pattern | Component Lookups | CPU Overhead | Frame Impact | Verdict |
|---|---|---|---|---|
| GetComponent in Update | 1 per frame | High | Can cause frame drops | [X] Bad |
| Cache GetComponent in Start | 1 at start | Low | Smooth frame rate | [OK] Good |