๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
ๅฐ้พ™่™พๅฐ้พ™่™พAI
๐Ÿค–

Unity

Avoid common Unity mistakes โ€” lifecycle ordering, GetComponent caching, physics timing, and Unity's fake null.

ไธ‹่ฝฝ2.5k
ๆ˜Ÿๆ ‡3
็‰ˆๆœฌ1.0.0
ๅผ€ๅ‘ๅทฅๅ…ท
ๅฎ‰ๅ…จ้€š่ฟ‡

ๆŠ€่ƒฝ่ฏดๆ˜Ž


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

  • Awake before Start โ€” use Awake for self-init, Start for cross-references
  • OnEnable called before Start โ€” but after Awake
  • Order between scripts not guaranteed โ€” use Script Execution Order if needed
  • Awake called even if disabled โ€” Start only when enabled

GetComponent Performance

  • GetComponent every frame is slow โ€” cache in Awake or Start
  • GetComponentInChildren searches recursively โ€” expensive on deep hierarchies
  • TryGetComponent returns bool โ€” avoids null check, slightly faster
  • Use RequireComponent attribute โ€” ensures dependency, documents requirement

Physics Timing

  • Physics in FixedUpdate, not Update โ€” consistent regardless of framerate
  • FixedUpdate can run 0 or multiple times per frame โ€” don't assume 1:1
  • Rigidbody.MovePosition in FixedUpdate โ€” transform.position bypasses physics
  • Time.deltaTime in Update, Time.fixedDeltaTime in FixedUpdate โ€” or just use deltaTime

Unity's Fake Null

  • Destroyed objects aren't truly null โ€” == null returns true, but object exists
  • Null-conditional ?. doesn't work properly โ€” use == null or bool conversion
  • Destroy doesn't happen immediately โ€” object gone next frame
  • Use DestroyImmediate only in editor โ€” causes issues in builds

Coroutines

  • StartCoroutine needs MonoBehaviour active โ€” disabled/destroyed stops coroutines
  • yield return null waits one frame โ€” yield return new WaitForSeconds(1) for time
  • StopCoroutine needs same method or Coroutine reference โ€” string overload unreliable
  • Can't return values โ€” use callbacks or set field in coroutine

Instantiate and Pooling

  • Instantiate is expensive โ€” pool frequently created/destroyed objects
  • Instantiate(prefab, parent) sets parent โ€” avoids extra SetParent call
  • SetActive(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 public
  • public fields 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
  • CreateAssetMenu attribute 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

  • Find methods every frame โ€” cache references
  • String comparisons for tags โ€” use CompareTag("Enemy"), not tag == "Enemy"
  • Physics queries allocate โ€” use NonAlloc variants: RaycastNonAlloc
  • UI anchors wrong โ€” stretches unexpectedly on different resolutions
  • async/await without context โ€” use UniTask or careful error handling

ๅฆ‚ไฝ•ไฝฟ็”จใ€ŒUnityใ€๏ผŸ

  1. ๆ‰“ๅผ€ๅฐ้พ™่™พAI๏ผˆWeb ๆˆ– iOS App๏ผ‰
  2. ็‚นๅ‡ปไธŠๆ–นใ€Œ็ซ‹ๅณไฝฟ็”จใ€ๆŒ‰้’ฎ๏ผŒๆˆ–ๅœจๅฏน่ฏๆก†ไธญ่พ“ๅ…ฅไปปๅŠกๆ่ฟฐ
  3. ๅฐ้พ™่™พAI ไผš่‡ชๅŠจๅŒน้…ๅนถ่ฐƒ็”จใ€ŒUnityใ€ๆŠ€่ƒฝๅฎŒๆˆไปปๅŠก
  4. ็ป“ๆžœๅณๆ—ถๅ‘ˆ็Žฐ๏ผŒๆ”ฏๆŒ็ปง็ปญๅฏน่ฏไผ˜ๅŒ–

็›ธๅ…ณๆŠ€่ƒฝ