0
0
Unityframework~8 mins

MonoBehaviour lifecycle in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: MonoBehaviour lifecycle
MEDIUM IMPACT
This affects the frame rendering speed and responsiveness of Unity games by controlling when and how often scripts run during the game loop.
Running code every frame efficiently
Unity
void Start() {
    // Precompute data once
    precomputedData = ComputeHeavyData();
}
void Update() {
    // Use precomputed data without allocations
    UseData(precomputedData);
}
Moves heavy work out of Update to Start, avoiding repeated costly operations and allocations.
📈 Performance GainReduces frame blocking to near zero during Update, smoother frame rate
Running code every frame efficiently
Unity
void Update() {
    // Heavy calculations or frequent allocations here
    for (int i = 0; i < 1000; i++) {
        var temp = new Vector3(i, i, i);
    }
}
Doing heavy work or allocating memory every frame causes frame rate drops and garbage collection spikes.
📉 Performance CostBlocks rendering for multiple milliseconds each frame, causing frame drops
Performance Comparison
PatternCPU UsageFrame DropsGarbage CollectionVerdict
Heavy work in UpdateHigh CPU every frameFrequent frame dropsHigh GC pressure[X] Bad
Precompute in Start, light UpdateLow CPU per frameSmooth framesMinimal GC[OK] Good
Rendering Pipeline
MonoBehaviour lifecycle methods run during the Unity game loop, affecting CPU usage and frame timing. Update and LateUpdate run every frame, FixedUpdate runs on fixed intervals for physics. Heavy work here delays rendering and input processing.
Script Execution
CPU Processing
Frame Rendering
⚠️ BottleneckScript Execution stage when heavy code runs in Update or FixedUpdate
Optimization Tips
1Avoid heavy or memory-allocating code inside Update or FixedUpdate.
2Use Start for one-time initialization to keep frame rendering smooth.
3Reuse objects and cache data to minimize garbage collection during gameplay.
Performance Quiz - 3 Questions
Test your performance knowledge
Which MonoBehaviour method is best for heavy initialization to avoid frame drops?
AStart
BUpdate
CFixedUpdate
DLateUpdate
DevTools: Profiler
How to check: Open Unity Profiler, record while running game, look at CPU Usage and Garbage Collection sections, check time spent in Update and FixedUpdate methods.
What to look for: High spikes or long durations in Update indicate performance issues; frequent GC collections show memory pressure.