๐ค
Unity
Avoid common Unity mistakes โ lifecycle ordering, GetComponent caching, physics timing, and Unity's fake null.
ๅฎๅ
จ้่ฟ
ๆ่ฝ่ฏดๆ
name: Unity description: Avoid common Unity mistakes โ lifecycle ordering, GetComponent caching, physics timing, and Unity's fake null. metadata: {"clawdbot":{"emoji":"๐ฎ","os":["linux","darwin","win32"]}}
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
ๅฆไฝไฝฟ็จใUnityใ๏ผ
- ๆๅผๅฐ้พ่พAI๏ผWeb ๆ iOS App๏ผ
- ็นๅปไธๆนใ็ซๅณไฝฟ็จใๆ้ฎ๏ผๆๅจๅฏน่ฏๆกไธญ่พๅ ฅไปปๅกๆ่ฟฐ
- ๅฐ้พ่พAI ไผ่ชๅจๅน้ ๅนถ่ฐ็จใUnityใๆ่ฝๅฎๆไปปๅก
- ็ปๆๅณๆถๅ็ฐ๏ผๆฏๆ็ปง็ปญๅฏน่ฏไผๅ