🦀 ClawHub
Unity
by @ivangdavila
Avoid common Unity mistakes — lifecycle ordering, GetComponent caching, physics timing, and Unity's fake null.
TERMINAL
clawhub install unity📖 About This Skill
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-referencesOnEnable called before Start — but after AwakeAwake called even if disabled — Start only when enabledGetComponent Performance
GetComponent every frame is slow — cache in Awake or StartGetComponentInChildren searches recursively — expensive on deep hierarchiesTryGetComponent returns bool — avoids null check, slightly fasterRequireComponent attribute — ensures dependency, documents requirementPhysics Timing
FixedUpdate, not Update — consistent regardless of framerateFixedUpdate can run 0 or multiple times per frame — don't assume 1:1Rigidbody.MovePosition in FixedUpdate — transform.position bypasses physicsTime.deltaTime in Update, Time.fixedDeltaTime in FixedUpdate — or just use deltaTimeUnity's Fake Null
== null returns true, but object exists?. doesn't work properly — use == null or bool conversionDestroy doesn't happen immediately — object gone next frameDestroyImmediate only in editor — causes issues in buildsCoroutines
StartCoroutine needs MonoBehaviour active — disabled/destroyed stops coroutinesyield return null waits one frame — yield return new WaitForSeconds(1) for timeStopCoroutine needs same method or Coroutine reference — string overload unreliableInstantiate and Pooling
Instantiate is expensive — pool frequently created/destroyed objectsInstantiate(prefab, parent) sets parent — avoids extra SetParent callSetActive(false) before returning to pool — not DestroySerialization
[SerializeField] for private fields in inspector — prefer over publicpublic fields auto-serialize — but exposes API you may not want[HideInInspector] hides but still serializes — [NonSerialized] to skip entirelyScriptableObjects
CreateAssetMenu attribute for easy creation — right-click → CreateCommon Mistakes
Find methods every frame — cache referencesCompareTag("Enemy"), not tag == "Enemy"NonAlloc variants: RaycastNonAllocasync/await without context — use UniTask or careful error handling