TExcelTAD
Home › Unity with C# › Unity Intro

Unity Intro

Unity is a game engine and editor. You build scenes out of objects in the editor, attach behaviour to them with C# scripts, and export the result to Windows, macOS, Linux, Android, iOS, the web and consoles. It is the most widely used engine for mobile and indie games, and it is used for VR, simulations and film previsualization as well.

This course targets Unity 6 (version numbers 6000.x), using the Universal Render Pipeline (URP) and the Input System package. If you learned Unity before 2023, some names have changed. They are flagged in the lessons with a "Migrating" note.

What you need first

  • C# basics: classes, properties, interfaces, generics, List<T>, events and lambdas. The C# course covers all of them.
  • The game loop, delta time and fixed timesteps from Game Development Roadmap & Fundamentals. Unity's Update and FixedUpdate are those ideas with engine names.
  • Vectors and dot products, from Math for Games.

How this course works

Unity can't run inside a web page lesson, so every topic comes in two forms:

  1. Real Unity code in plain csharp blocks. You paste it into a script in your own project. It uses current Unity 6 APIs.
  2. A runnable model of the same idea in plain C#, which runs on this site (.NET, through Compiler Explorer). The model removes the engine and keeps the mechanism: the lifecycle order, how "fake null" works, how a pool recycles objects, how an ECS iterates arrays.

Here is the smallest version of the trick. Unity calls methods on your components; you never write Main. A model of that is a loop that calls them for you:

csharp · try it yourself
using System;
using System.Collections.Generic;

var scene = new List<Behaviour> { new Spinner(), new Spinner { Speed = 180 } };
float deltaTime = 0.5f;                       // pretend each frame takes half a second

foreach (var b in scene) b.Start();
for (int frame = 1; frame <= 3; frame++)
{
    foreach (var b in scene) b.Update(deltaTime);
    Console.WriteLine(
quot;frame {frame}: " + string.Join(", ", scene)); } abstract class Behaviour { public virtual void Start() { } public virtual void Update(float dt) { } } class Spinner : Behaviour { public float Speed = 90; // degrees per second, like a public Inspector field float angle; public override void Start() => angle = 0; public override void Update(float dt) => angle = (angle + Speed * dt) % 360; public override string ToString() =>
quot;{angle:F0} deg"; }
frame 1: 45 deg, 90 deg
frame 2: 90 deg, 180 deg
frame 3: 135 deg, 270 deg

And here is the same component in Unity:

csharp
using UnityEngine;

public class Spinner : MonoBehaviour
{
    [SerializeField] float speed = 90f;      // degrees per second, editable in the Inspector

    void Update()
    {
        transform.Rotate(0f, speed * Time.deltaTime, 0f);
    }
}

The engine finds every enabled MonoBehaviour in the loaded scenes and calls Update once per frame. Time.deltaTime is the frame time in seconds.

Your first project

  1. Install Unity Hub, then a Unity 6 LTS editor through it (see Unity Hub, Modules & IDEs).
  2. In the Hub, choose New project and pick the Universal 3D template (named "3D (URP)" in some versions). It sets up URP, the Input System and a sample scene.
  3. Name the project, choose a folder outside cloud-synced directories (OneDrive and Dropbox fight with Unity's file writes) and create it.
  4. In the Project window, right-click Assets, choose Create, then MonoBehaviour Script (newer 6.x releases group it under Scripting), and call it Spinner. The class name must match the file name.
  5. Paste the code above, save, and wait for Unity to recompile (the spinner in the bottom-right corner).
  6. Add a cube (GameObject > 3D Object > Cube), drag the script onto it, and press Play.

What's in the course

Section You learn
Editor, GameObjects, Scripting scenes, components, prefabs, the lifecycle, serialization, coroutines, Awaitable, events
Input and Physics the Input System, rebinding, Rigidbody, colliders, raycasts, CharacterController
Data, UI, Audio, 3D ScriptableObjects, saving, Addressables, UI Toolkit, AudioMixer, URP, Cinemachine, Animator, NavMesh, VFX
Architecture and Performance event channels, services, state machines, tests, the Profiler, GC, pooling
DOTS, Multiplayer, Shipping ECS, Jobs, Burst, Netcode for GameObjects, build profiles, IL2CPP
Toolkit and Field notes packages, IDEs, Asset Store tools, version control, hard-won advice

You finish with Crystal Run, a small 3D collect-and-escape game.

Test yourself

Which Unity version line does this course target?

  1. Unity 2019 LTS
  2. Unity 6 (6000.x)
  3. Unity 5
  4. Unity 2021 with the old Input Manager

Who calls a MonoBehaviour's Update method?

  1. Your Main method
  2. The engine, once per frame for every enabled component
  3. Only the Inspector
  4. The physics system, 50 times per second

Why does each Unity topic in this course also have a plain C# model?

  1. Unity code is slower
  2. The site can run plain C#, so the model shows the mechanism in a form you can execute
  3. Unity does not use C#
  4. Models replace Unity in production