Unity C# Fundamentals
Use This Skill When
- Writing Unity Editor code or Unity-facing examples.
- Adding components, ScriptableObjects, asset operations, scene operations, or editor utilities.
- Reviewing Unity API usage for safety.
Core Rules
- Follow
AGENTS.mdandcsharp-code-style. - Keep Unity API calls on the main thread unless Unity explicitly documents otherwise.
- Use
[SerializeField] privatefields for inspector configuration when needed. - Use
[RequireComponent]when a component has a hard runtime dependency. - Use
TryGetComponentwhen the component may be absent. - Cache repeated component lookups only when the lookup happens repeatedly or in a hot path.
- Avoid hidden global lookups unless the task requires project-wide discovery.
- Do not use
switch.
Lifecycle
- Keep
Awake,OnEnable,Start,Update, andOnDisablesmall. - Subscribe in
OnEnableand unsubscribe inOnDisablewhen the object lifetime allows it. - Use
OnDestroyfor cleanup that must happen even if the object was never enabled. - Do not start async work or coroutines without a cancellation or stop path.
Editor Code
- Editor-only code may use
UnityEditorand belongs in an Editor assembly or Editor folder. - Validate assets, scenes, and object IDs before mutating them.
- Prefer Unity APIs over hand-editing serialized text.
- Refresh live instance IDs after compile, scene load, domain reload, or asset import.
References
Legacy reference files remain under references/. Use only parts that match current AGENTS.md.