30 Free Code Snippets for Unity Game Development (Updated May 2026)
Essential Unity C# code snippets and script examples for 2026. Movement, input, UI, physics, DOTS, and performance patterns. Copy-paste ready for faster development.
New Input System - Action-Based Movement
Input SystemUnity Input System 2.0 movement with actions and binding.
Perfect for: cross-platform, rebindable controls
Smooth Camera Follow (Lerp)
CameraFrame-rate independent camera follow with damping and bounds.
Perfect for: 2D/3D platformers, top-down
Generic Object Pool
PerformanceReusable object pool for bullets, particles, and enemies. Reduces allocations.
Perfect for: performance, mobile
Health and Damage Events
Game SystemsEvent-driven health system with OnDamage, OnHeal, OnDeath and optional shields.
Perfect for: RPGs, action games
Coroutine-Based Timer
Game MechanicsCountdown and delay without Update. Cooldowns, respawn, and sequences.
Perfect for: abilities, game flow
Screen Shake (Cinemachine or Custom)
Visual EffectsCamera shake on impact or events for juice and feedback.
Perfect for: combat, explosions
Async Scene Loading with Progress
Scene ManagementSceneManager.LoadSceneAsync with progress bar and minimum display time.
Perfect for: loading screens, large levels
PlayerPrefs Save/Load Wrapper
Data ManagementSimple persistence for settings, high scores, and progress with type-safe helpers.
Perfect for: settings, unlocks
Simple State Machine (AI or Game)
AI SystemEnum-based or class-based states for enemy AI, UI, or game flow.
Perfect for: AI, menus
UI Button Sound and Hover
UI SystemCentralized UI audio and hover feedback for menus and HUD.
Perfect for: polish, accessibility
Raycast Ground Check (2D/3D)
MovementGrounded check for platformers and character controllers using Raycast or Raycast2D.
Perfect for: jumping, slopes
Singleton Service Locator (Optional)
ArchitectureSimple service access for GameManager, AudioManager, or SaveManager. Use sparingly.
Perfect for: global services
Event Channel (ScriptableObject Events)
ArchitectureDecoupled events via ScriptableObjects for UI, achievements, and systems.
Perfect for: loose coupling, scalability
FPS Counter (OnGUI or UI)
PerformanceReal-time FPS display for profiling and build verification.
Perfect for: optimization, QA
Safe Destroy (Null Check and Delay)
UtilitiesDestroy with null check and optional delay for effects or cleanup.
Perfect for: VFX, pool return