Web and App Projects

Building a Custom Game with Unity and C#

How a game engine organises scenes, objects and scripts, and how to plan and scope a small first game, with only short illustrative C# ideas.

On this page
  1. What a game engine does for you
  2. How the engine organises a game
  3. Scripts: the moving parts
  4. Planning a small first game
  5. Using version control and good habits
  6. Assets, licences and credits
  7. Performance, testing and sharing
  8. Realistic expectations

What a game engine does for you

A game engine is a toolkit that handles the repetitive, difficult parts of making a game so you can concentrate on the parts that make yours different. It draws graphics, plays audio, reads input from keyboards, controllers and touch screens, simulates physics, loads assets and runs your logic every frame. Unity is one widely used engine, and C# is the language used to write its scripts.

Because the engine owns the main loop, you rarely write a program that starts at the top and runs to the end. Instead, you describe objects and give them behaviour, and the engine calls your code at the right moments. Understanding that shift is the first step.

Details of menus, versions and packages change over time, so rely on the engine’s current documentation for exact steps. This article is about the concepts that persist.

How the engine organises a game

Scenes

A scene is a container for a part of the game: a level, a menu, a title screen. Loading a scene brings in everything inside it. Small games often have a handful of scenes, such as a main menu, one or more levels and a results screen.

Game objects and components

Everything in a scene is a game object, and an object on its own does very little. You give it abilities by attaching components. One component positions it in space, another draws a shape, another gives it physics behaviour, another plays a sound. This design favours combining small pieces over building deep inheritance chains.

A script you write is also a component. Attaching it to an object gives that object your custom behaviour.

Assets and prefabs

Assets are the files your game uses: images, models, sounds, animations. A prefab is a reusable template for an object, such as an enemy or a coin. Change the template and every copy updates, which is how you avoid tweaking fifty identical objects by hand.

Physics and collisions

The engine can move objects according to physical rules and tell you when two objects touch. You decide what that contact means: lose health, collect an item, open a door. Treat physics as a tool that is useful but must be tuned, because default values rarely feel right.

Scripts: the moving parts

A script typically defines a class with functions the engine calls at particular times: once when the object is created, once per frame, at fixed time intervals for physics, and when collisions happen. Your task is to put the right logic in the right function.

A tiny illustration of the idea, in the style of a typical engine script:

void Update()
{
    // runs once per frame: read input, move the object
}

Two habits help right away.

Think in time, not in loops. Because code runs every frame, a movement amount should usually be scaled by the time elapsed since the last frame, so the game behaves the same on a fast or slow device. Writing a while loop that waits for something to happen would freeze the whole game.

Expose settings, hide complexity. Values such as speed or jump strength can be adjustable in the editor rather than fixed inside the code, so you can tune the feel of the game without recompiling. Keep scripts small and give them one clear job. The same advice as in readable code applies here.

For game logic with several modes, such as a character who is idle, running, jumping or falling, a state machine keeps behaviour manageable. The concept is explained in state machines for device behavior, and it translates directly to game characters, enemies and menus.

Planning a small first game

Most first games are never finished, because the idea was too big. Scope control is the most important skill you can practise.

  1. Pick one core action. Jumping, dodging, matching, collecting, aiming. If it is not fun in its simplest form, extra features will not rescue it.
  2. Write a one-sentence description. For example, “a player avoids falling objects and collects points”. If you cannot say it in a sentence, it is probably too large.
  3. List the minimum. What must exist for the game to be playable from start to end? Usually: a controllable player, one challenge, a way to win or lose, and a restart.
  4. Make a rough version quickly. Use plain shapes and temporary sounds. Prove that the core action works before spending time on art.
  5. Add features one at a time, and keep a separate list of “nice to have” ideas you can ignore for now.
  6. Set a time limit. A game finished in a few weeks teaches more than one abandoned after six months.

Games that are a good fit for a first project are small, rule-based and 2D, such as a simple arcade game or a puzzle. Multiplayer, open worlds and complex artificial intelligence are rarely sensible first goals. If enemy behaviour is needed, simple rules usually go a long way.

Using version control and good habits

Game projects contain many files, and mistakes are common, so keep the project under version control from the first day. Check what the engine recommends for which files to track and which to ignore, since generated files should not be stored. Commit when something works.

When something misbehaves, apply a clear debugging strategy: reproduce the problem, check one assumption at a time, and use the engine’s console and visual debugging aids. Small, regular playtests reveal problems that you cannot see in the editor. Letting a friend play without instructions is especially revealing.

Assets, licences and credits

You do not have to create everything yourself. Free and paid asset libraries exist, but every asset has a licence that states what you may do with it, including whether you may sell your game or must credit the author. Read the licence before using an asset, and keep a list of what you used and where it came from. If you plan to publish your own code or assets, choose a licence deliberately; software licenses explains the usual options.

Performance, testing and sharing

Performance matters even in small games. Avoid doing heavy work every frame, reuse objects rather than creating and destroying many, and test on the device you intend to target, since an editor on a powerful computer can hide problems. Where parts of your logic can be separated from the engine, simple automated testing can catch mistakes early, though many hobby projects rely mostly on playtesting.

When you are ready to share, the engine can build for several platforms. Each platform has its own requirements for packaging, store accounts and age or content rules, so read the current documentation for the one you choose. If your game handles player data, accounts or online features, think carefully about privacy and security before releasing it.

Realistic expectations

A first game will probably look rough and play simply. That is fine. The goal is to get through the full loop of idea, prototype, polish and release at least once. Every later project will be easier. If the idea of structuring objects with behaviour appeals, you will find it also helps in other kinds of software, including mobile apps and web applications.

The complete guide to web and app projects

A Guide to Building a Custom Website with HTML, CSS, and JavaScript
Read the guide

Keep exploring

Guides from other topics

All topics →