unity-csharp-scripting
gamedev-skills/awesome-gamedev-agent-skills
Write correct Unity 6 MonoBehaviour scripts with proper lifecycle, component caching, and coroutines.
What is unity-csharp-scripting?
Master the MonoBehaviour lifecycle (Awake/Start/Update/FixedUpdate/LateUpdate), component access patterns, Inspector serialization, and coroutines for deterministic gameplay logic. Use this when authoring or fixing .cs scripts in a Unity project.
- Choose the right lifecycle callback by purpose (Awake for caching, Start for cross-object init, Update for per-frame logic, FixedUpdate for physics)
- Cache component lookups in Awake to avoid GetComponent calls every frame
- Expose tunables with [SerializeField] private so the Inspector stays editable but code stays encapsulated
- Scale movement and timing by Time.deltaTime for frame-rate independence
- Write time-sequenced logic with coroutines (delays, tweens, conditional waits)
- Verify behaviour in Play mode: check Console, inspect values, profile hot Update calls
How to install unity-csharp-scripting
npx skills add https://github.com/gamedev-skills/awesome-gamedev-agent-skills --skill unity-csharp-scripting- Unity 6.3 LTS (6000.3) or later
- A Unity project with Assembly-CSharp or .asmdef files
- C# / .NET Standard 2.1 support
How to use unity-csharp-scripting
- 1.Identify the callback you need: Awake (one-time setup), Start (cross-object init), Update (per-frame), FixedUpdate (physics), LateUpdate (post-movement), OnDisable (cleanup)
- 2.Cache component references in Awake using GetComponent or TryGetComponent, store in private fields
- 3.Expose tunables with [SerializeField] private and optional attributes like [Range] or [System.Serializable]
- 4.Scale movement by Time.deltaTime in Update and Time.fixedDeltaTime in FixedUpdate
- 5.Use StartCoroutine for time-sequenced logic; yield return WaitForSeconds or null to resume
- 6.Test in Play mode: check the Console for null-reference errors, verify Inspector values update, and profile if Update is a bottleneck
Use cases
- Building a PlayerController that reads input, rotates the camera, and applies physics-based movement
- Implementing a timed sequence (flash effect, wait, disable renderer) with coroutines
- Caching a Rigidbody or Animator reference on load to avoid repeated GetComponent calls
- Exposing designer-tunable values (moveSpeed, jumpForce, volume) in the Inspector without exposing them to other code
- Subscribing to events in OnEnable and unsubscribing in OnDisable for clean lifecycle management
- Game programmers writing gameplay scripts in Unity
- Level designers who need to tune script parameters in the Inspector
- Developers migrating from older Unity versions to Unity 6.3 LTS
- Anyone building MonoBehaviour-based gameplay logic
unity-csharp-scripting FAQ
Use Awake to cache component references and initialize state that doesn't depend on other objects. Use Start for initialization that depends on other objects' Awake calls. Start runs after all Awakes are complete, so cross-object wiring is safe in Start.
GetComponent searches the component list every time, which is O(n) and wastes CPU. Cache the reference once in Awake and reuse it. If you need optional components, use TryGetComponent once and store the result.
Use [SerializeField] private. This hides the field from other scripts while exposing it to the Inspector. Avoid public fields unless you specifically want other code to mutate them.
No. Physics calculations run on a fixed timestep in FixedUpdate. Moving a Rigidbody in Update causes jitter and frame-rate-dependent behaviour. Read input in Update, apply physics in FixedUpdate.
Coroutines stop when the GameObject is disabled. If a coroutine must survive toggling, restart it in OnEnable or use a manager object that stays active.
Full instructions (SKILL.md)
Source of truth, from gamedev-skills/awesome-gamedev-agent-skills.
name: unity-csharp-scripting description: > Write Unity 6.3 LTS C# gameplay scripts: the MonoBehaviour lifecycle (Awake/OnEnable/Start/Update/FixedUpdate/LateUpdate), GameObject and component access, coroutines, and Inspector serialization. Use when creating or editing .cs scripts in a Unity project, or when the user mentions MonoBehaviour, Start/Update, GetComponent, SerializeField, coroutines, or "Unity script".
Unity C# Scripting (MonoBehaviour)
Write correct, idiomatic gameplay scripts in Unity 6. Get the lifecycle, component access, serialization, and coroutines right so behaviour is deterministic and the Inspector stays useful. Targets Unity 6.3 LTS (6000.3), C# / .NET Standard 2.1.
When to use
- Use when authoring or fixing a
MonoBehaviour: choosing the right lifecycle callback, reading/caching components, exposing fields to the Inspector, or running timed logic with coroutines. - Use when the project has
*.csfiles, anAssembly-CSharpor*.asmdef, and aProjectSettings/folder.
When not to use: moving rigidbodies / collision response → unity-physics; reading
player input → unity-input-system; shared data assets / config → unity-scriptableobjects;
Animator parameters → unity-animation. This skill owns the script lifecycle and C#
plumbing, not those subsystems.
Core workflow
- Pick the callback by purpose, not habit.
Awake(cache references, runs once on load),OnEnable(subscribe to events),Start(init that depends on other objects'Awake),Update(per-frame logic/input polling),FixedUpdate(physics),LateUpdate(camera follow after movement),OnDisable/OnDestroy(unsubscribe/cleanup). - Cache component lookups in
Awake— never callGetComponentevery frame. - Expose tunables with
[SerializeField] private, not public fields, so other code can't mutate them but designers can edit them in the Inspector. - Scale per-frame values by
Time.deltaTimeinUpdate(andTime.fixedDeltaTimesemantics are automatic inFixedUpdate). - Use coroutines for time-sequenced logic (delays, tweens, "do X then wait then Y");
start them with
StartCoroutineand stop them deterministically. - Verify in Play mode: check the Console for null-reference exceptions, confirm values
in the Inspector update as expected, and watch the Profiler if
Updateis hot.
Patterns
1. Lifecycle + cached components (the canonical skeleton)
using UnityEngine;
[RequireComponent(typeof(Rigidbody))] // auto-adds the dependency, prevents null refs
public class PlayerController : MonoBehaviour
{
[SerializeField] private float moveSpeed = 6f; // editable in Inspector, private in code
private Rigidbody _rb; // cached, not fetched per frame
private void Awake() => _rb = GetComponent<Rigidbody>(); // cache once on load
private void Update()
{
// Per-frame, non-physics work. Scale by deltaTime so it is frame-rate independent.
transform.Rotate(0f, 90f * Time.deltaTime, 0f);
}
private void FixedUpdate()
{
// Physics work belongs here (fixed timestep). See the unity-physics skill.
_rb.MovePosition(_rb.position + transform.forward * moveSpeed * Time.fixedDeltaTime);
}
}
2. Safe component access with TryGetComponent
// Avoids allocating a null and is clearer than GetComponent + null check.
if (other.TryGetComponent<Health>(out var health))
health.Apply(-10);
3. Serialization that shows up correctly in the Inspector
[SerializeField, Range(0f, 1f)] private float volume = 0.8f; // slider
[SerializeField] private string playerName = "Hero"; // private but serialized
[System.Serializable] // REQUIRED for a plain class to serialize/show
public class Stats { public int hp = 100; public int mana = 50; }
[SerializeField] private Stats stats = new(); // nested struct-like data in the Inspector
4. Coroutines for time-sequenced logic
private void Start() => StartCoroutine(FlashThenHide());
private System.Collections.IEnumerator FlashThenHide()
{
yield return new WaitForSeconds(0.5f); // wait half a second of game time
GetComponent<Renderer>().enabled = false;
yield return null; // resume next frame
}
Pitfalls
GetComponentinUpdate— it searches every frame and tanks performance. Cache the reference inAwake/Start.- Physics in
Update— moving aRigidbodywith forces orMovePositionoutsideFixedUpdatecauses jitter and timestep-dependent behaviour. Read input inUpdate, apply physics inFixedUpdate. - Relying on
Startorder across objects —Startruns after allAwakes, but order amongStarts is undefined. Do cross-object wiring inStart, self-setup inAwake. publicfields just to show them in the Inspector — that also lets any script mutate them. Use[SerializeField] privateinstead.gameObject.tag == "Enemy"allocates a string and is slower; usegameObject.CompareTag("Enemy").- Coroutines stop when the GameObject is disabled — a disabled object's coroutines are
killed; re-
StartCoroutineinOnEnableif it must survive toggling. Updatenever runs beforeStart, but the firstUpdatecan run on the same frame asStart— guard against not-yet-initialised fields if you split setup oddly.
References
- For the full event-execution-order table and advanced coroutine patterns (custom
CustomYieldInstruction, stopping by handle,WaitUntil/WaitWhile), readreferences/lifecycle-and-coroutines.md. - Primary docs: Unity Manual "Event function execution order"
(
https://docs.unity3d.com/Manual/execution-order.html) andScriptReference/MonoBehaviour.
Related skills
unity-physics—Rigidbody, collisions, andFixedUpdatemotion.unity-input-system— reading player input into these scripts.unity-scriptableobjects— sharing data/config between scripts without singletons.
Related skills
More from gamedev-skills/awesome-gamedev-agent-skills and the wider catalog.

unity-input-system
Wire player input in Unity 6.3 LTS with action-based, device-agnostic Input System package.

unity-navmesh
Add AI pathfinding to Unity 6.3 LTS with NavMesh baking and agent steering.

unity-physics
Set up 3D physics in Unity 6.3 LTS: Rigidbody movement, colliders, collisions, raycasts, and joints.

unity-scriptableobjects
Design data-driven Unity systems with ScriptableObject assets for config, events, and registries without singletons.

unity-tilemap-2d
Build and script 2D tilemaps in Unity 6.3 LTS with Grid, Tilemap, colliders, and rule tiles.

unreal-behavior-trees
Build NPC AI in Unreal Engine 5 with Behavior Trees, Blackboards, and AIControllers.