gamedev-skills/awesome-gamedev-agent-skills

unity-csharp-scripting

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.

View source
Original skill document

Rendered from the source repository. Headings, examples, code, tables, links, and referenced images are preserved.

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 *.cs files, an Assembly-CSharp or *.asmdef, and a

ProjectSettings/ 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

  1. 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).

  1. Cache component lookups in `Awake` — never call GetComponent every frame.
  2. Expose tunables with `[SerializeField] private`, not public fields, so other code

can't mutate them but designers can edit them in the Inspector.

  1. Scale per-frame values by `Time.deltaTime` in Update (and Time.fixedDeltaTime

semantics are automatic in FixedUpdate).

  1. Use coroutines for time-sequenced logic (delays, tweens, "do X then wait then Y");

start them with StartCoroutine and stop them deterministically.

  1. Verify in Play mode: check the Console for null-reference exceptions, confirm values

in the Inspector update as expected, and watch the Profiler if Update is hot.

Patterns

1. Lifecycle + cached components (the canonical skeleton)

csharp
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

csharp
// 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

csharp
[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

csharp
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

  • `GetComponent` in `Update` — it searches every frame and tanks performance. Cache the

reference in Awake/Start.

  • Physics in `Update` — moving a Rigidbody with forces or MovePosition outside

FixedUpdate causes jitter and timestep-dependent behaviour. Read input in Update, apply physics in FixedUpdate.

  • Relying on `Start` order across objectsStart runs after all Awakes, but order

among Starts is undefined. Do cross-object wiring in Start, self-setup in Awake.

  • `public` fields just to show them in the Inspector — that also lets any script mutate

them. Use [SerializeField] private instead.

  • `gameObject.tag == "Enemy"` allocates a string and is slower; use

gameObject.CompareTag("Enemy").

  • Coroutines stop when the GameObject is disabled — a disabled object's coroutines are

killed; re-StartCoroutine in OnEnable if it must survive toggling.

  • `Update` never runs before `Start`, but the first* Update can run on the same

frame as Start** — 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), read references/lifecycle-and-coroutines.md.

  • Primary docs: Unity Manual "Event function execution order"

(https://docs.unity3d.com/Manual/execution-order.html) and ScriptReference/MonoBehaviour.

Related skills

  • unity-physicsRigidbody, collisions, and FixedUpdate motion.
  • unity-input-system — reading player input into these scripts.
  • unity-scriptableobjects — sharing data/config between scripts without singletons.
from this repository

More skills

All skills
gamedev-skills
Community

game-feel

Add "juice" and game feel that makes actions satisfying — screen shake, hit-stop/freeze frames, tweened/eased motion, squash & stretch, knockback, and layered audio-visual feedback — as engine-neutral techniques that pair with the detected engine's tween, particle, and camera APIs. Use when the user mentions game feel, juice, "make it feel good/punchy", screen shake, hit stop, screen freeze, easing, squash and stretch, impact frames, or feedback/polish on hits, jumps, pickups, and deaths.

installs
5
GitHub stars
1.1K
Updated
Sep 10
gamedev-skills
Community

game-ui-ux

Design and build game UI/UX — HUDs, menus, and overlays — that survive every screen: anchor- based responsive layout, resolution/aspect scaling and safe areas, keyboard/gamepad focus navigation, a screen/menu state stack, and event-driven (not polled) HUD updates. Engine- neutral patterns that pair with the detected engine's UI skill. Use when the user mentions HUD, health bar, main menu, pause menu, settings screen, UI layout, anchors, UI scaling, aspect ratio, safe area, controller/keyboard menu navigation, or wiring UI to game state.

installs
5
GitHub stars
1.1K
Updated
Sep 10
gamedev-skills
Community

godot-3d-essentials

Set up a Godot 4.7 3D scene: Node3D transforms, Camera3D, lighting (DirectionalLight3D/OmniLight3D), WorldEnvironment for sky/ambient/tonemap/post, MeshInstance3D materials, and GridMap for tile-based 3D levels. Use when building a 3D scene in a Godot project, placing cameras/lights, configuring environment and post-processing, or working with Node3D/.tscn 3D content and GridMap.

installs
2
GitHub stars
1.1K
Updated
Sep 10
gamedev-skills
Community

godot-animation

Animate in Godot 4.7 three ways: AnimationPlayer for keyframed clips (incl. call and signal tracks), AnimationTree with state machines and blend spaces for character animation, and Tween for short procedural/UI tweens via createtween(). Use when working with AnimationPlayer/AnimationTree nodes in a .tscn, blending character states, sprite-sheet animation, or code-driven Tweens.

installs
2
GitHub stars
1.1K
Updated
Sep 10