unity-scripting
Installation
SKILL.md
Unity C# Scripting
Script Fundamentals
C# scripts (.cs files) are stored in the Assets folder. Scripts gain Unity functionality by inheriting from built-in types:
- UnityEngine.Object -- Makes custom types assignable to Inspector fields
- MonoBehaviour -- Attaches to GameObjects as components to control behavior in a scene
- ScriptableObject -- Standalone data assets not attached to GameObjects
Scripts operate in two contexts:
- Runtime scripts -- Execute in the Player build (use
UnityEnginenamespace) - Editor scripts -- Run only in the Editor (use
UnityEditornamespace, place inEditorfolders)
MonoBehaviour Lifecycle
MonoBehaviours always exist as a Component of a GameObject. The lifecycle event functions execute in a strict order. You cannot rely on the order in which the same event function is invoked for different GameObjects unless configured via Script Execution Order settings.