Lifecycle Order
AwakebeforeStart— use Awake for self-init, Start for cross-referencesOnEnablecalled beforeStart— but afterAwake- Order between scripts not guaranteed — use Script Execution Order if needed
Awakecalled even if disabled —Startonly when enabled
GetComponent Performance
GetComponentevery frame is slow — cache inAwakeorStartGetComponentInChildrensearches recursively — expensive on deep hierarchiesTryGetComponentreturns bool — avoids null check, slightly faster- Use
RequireComponentattribute — ensures dependency, documents requirement
Physics Timing
- Physics in
FixedUpdate, notUpdate— consistent regardless of framerate FixedUpdatecan run 0 or multiple times per frame — don't assume 1:1Rigidbody.MovePositionin FixedUpdate —transform.positionbypasses physicsTime.deltaTimein Update,Time.fixedDeltaTimein FixedUpdate — or just use deltaTime
Unity's Fake Null
- Destroyed objects aren't truly null —
== nullreturns true, but object exists - Null-conditional
?.doesn't work properly — use== nullorboolconversion Destroydoesn't happen immediately — object gone next frame- Use
DestroyImmediateonly in editor — causes issues in builds
Coroutines
StartCoroutineneeds MonoBehaviour active — disabled/destroyed stops coroutinesyield return nullwaits one frame —yield return new WaitForSeconds(1)for timeStopCoroutineneeds same method or Coroutine reference — string overload unreliable- Can't return values — use callbacks or set field in coroutine
Instantiate and Pooling
Instantiateis expensive — pool frequently created/destroyed objectsInstantiate(prefab, parent)sets parent — avoids extra SetParent callSetActive(false)before returning to pool — not Destroy- Pool inactive objects under a parent — keeps hierarchy clean
Serialization
[SerializeField]for private fields in inspector — prefer over publicpublicfields auto-serialize — but exposes API you may not want[HideInInspector]hides but still serializes —[NonSerialized]to skip entirely- Serialized fields keep inspector values — code defaults ignored after first serialize
ScriptableObjects
- Data containers that live as assets — share between scenes/prefabs
CreateAssetMenuattribute for easy creation — right-click → Create- Don't modify at runtime in builds — changes not saved (except in editor)
- Great for config, item databases — reduces prefab duplication
Common Mistakes
Findmethods every frame — cache references- String comparisons for tags — use
CompareTag("Enemy"), nottag == "Enemy" - Physics queries allocate — use
NonAllocvariants:RaycastNonAlloc - UI anchors wrong — stretches unexpectedly on different resolutions
async/awaitwithout context — use UniTask or careful error handling