Book a free call

How Does Component-Based Architecture Work in Unity?

Unity builds games from GameObjects with small components attached, instead of deep class trees. Here is how that design works and how to use it well.

Hertiv engineering teamReviewed by a senior engineer
7 min read
How Does Component-Based Architecture Work in Unity?
On this page

Short answer

Component-based architecture in Unity builds a game from GameObjects, each made of small components that add one behaviour, such as movement, health or rendering. Instead of a deep class tree, you compose behaviour by attaching components, which keeps each piece small, reusable and easy to test on its own.

What is component-based architecture in Unity?

Component-based architecture in Unity is a design where a game is built from GameObjects, and each GameObject gets its behaviour from small components attached to it, rather than from a deep class tree. A player character, for example, is a GameObject with a movement component, a health component, a rendering component and an input component.

Each component does one job and knows little about the others. The movement component moves the character, the health component tracks damage, and the rendering component draws the sprite or model. If you want a new kind of character, you attach a different set of components instead of writing a new subclass.

This is called composition over inheritance. Inheritance says a character is a kind of thing, which forces shared behaviour into a class tree that grows tangled. Composition says a character has a set of parts, and you change the character by changing the parts.

How do GameObjects and components fit together?

GameObjects and components fit together as a simple pair: a GameObject is a container in the scene, and components are the parts that give it behaviour. Every GameObject has a Transform component that stores its position, rotation and scale, and you add the rest yourself.

PartWhat it doesExample
GameObjectHolds componentsPlayer, enemy, coin
TransformPosition and rotationEvery object has one
MonoBehaviourRuns your codeMovement, health scripts
Built-in componentEngine featureRenderer, collider, audio
PrefabReusable templateEnemy type, bullet

Scripts in Unity are components too. When you write a script that inherits from MonoBehaviour and attach it to a GameObject, it becomes a component with its own fields, which you can set in the editor without touching code.

Prefabs turn a finished GameObject into a template. You build one enemy with all its components, save it as a prefab, and spawn copies that share the same setup. Change the prefab and every copy updates, which is how small teams keep a game consistent.

Why does composition beat inheritance in games?

Composition beats inheritance in games because game objects rarely fit a neat family tree. A flying enemy, a swimming enemy and a boss may share some behaviour and differ in others, and a class tree forces you to pick one branch for each, then duplicate the shared parts or push them into awkward base classes.

With components, each behaviour is a separate part. A flying enemy gets a flight component, a swimming enemy gets a swim component, and a boss gets both plus a health bar component. No behaviour is copied, and no object carries code it never uses.

Composition also makes changes cheaper. Adding a new ability means writing one component and attaching it where needed, instead of editing a base class and checking every subclass still works. Removing an ability is the same in reverse: detach the component and the object loses that behaviour without a code change.

How do components talk to each other?

Components talk to each other in three main ways, and each suits a different kind of message. The simplest is a direct reference: one component holds a reference to another on the same GameObject and calls its methods, which works well when the two parts always appear together.

The second way is events. A health component can raise an event when damage is taken, and any component that cares, such as a sound player or a health bar, listens for it. Events keep components apart, so the health component never needs to know who is listening.

The third way is shared data through ScriptableObjects. A ScriptableObject is an asset that lives outside any scene and holds values or events that many objects can read. A team might keep player stats or game settings in one ScriptableObject, so designers can change numbers in the editor without opening code.

What does Unity's ECS add on top?

Unity's ECS, or Entity Component System, is a stricter form of the same idea, built for speed. In the classic model, each GameObject carries MonoBehaviour scripts that run one at a time. In ECS, data lives in components, logic lives in systems, and the engine runs systems across many entities at once.

ECS is part of DOTS, Unity's data-oriented technology stack, which also includes the Job System and the Burst compiler. The Job System splits work across CPU cores, and Burst turns the code into fast native instructions. Together they suit games with thousands of similar objects, such as crowds, bullets or particles.

Most small games do not need ECS at all. The classic GameObject model is easier to learn, easier to debug and fast enough for hundreds of objects. ECS pays off when profiling shows the classic model is the bottleneck, and moving to it means rewriting how objects store data and how systems process them.

How do you build a game with components in 6 steps?

You build a game with components by starting with the smallest set of parts that can move and act, then adding one behaviour at a time. The steps below fit a small team working in Unity on a first game.

  1. List the behaviours, not the objects. Write down what the game must do: move, jump, take damage, collect, shoot. Each behaviour becomes a component.
  2. Build one GameObject per thing. Create the player, one enemy and one pickup, each with only the components it needs to work.
  3. Keep each component small. One component should do one job, so a movement script never handles health or scoring.
  4. Connect parts with events. Use events or ScriptableObjects for messages between parts, so components stay independent.
  5. Turn repeats into prefabs. Once an object works, save it as a prefab and spawn copies instead of rebuilding it by hand.
  6. Test parts on their own. Attach a component to a test object and check it works before it joins the full game.

Resist the urge to write a big manager script that knows everything. A manager that controls movement, health, scoring and spawning becomes the same tangled class the component model was meant to replace.

Example (hypothetical): a small team rebuilds its player controller

Example (hypothetical): a team of four builds a platform game, and the player controller has grown into one script of 1,200 lines that handles movement, jumping, health, animation and scoring. Every new feature touches the same file, and each of the next 8 features takes 3 days, or 24 days in total.

The team splits the script into 6 components: movement, jumping, health, animation, scoring and input. Each component is under 200 lines and does one job, so a new feature touches one component instead of the whole file.

With the split done, each of the 8 features takes 1.5 days, or 12 days in total. The team saves 12 days, which is about two and a half working weeks, and the two bugs found during the split were both in code that two features had been sharing by accident.

The split itself took 4 days, so the net saving on this batch of features is 8 days. The team keeps the split because the next batch of features will save time too, and a new developer can now read one component instead of the whole controller.

What are the common mistakes?

The most common component mistakes in Unity are giant scripts that do everything, components that depend on each other too tightly, and splitting too early into too many parts.

  • One script does everything. A controller that handles movement, health and scoring is a class tree in disguise, so split it by behaviour.
  • Tight coupling. A component that reaches into three others breaks when any of them changes, so use events or shared data instead.
  • Too many tiny parts. Splitting a simple jump into four components adds overhead, so split where behaviour is reused or changed often.
  • Logic in the editor. Settings belong in the editor, but rules belong in code, so keep decisions out of inspector fields.
  • Skipping prefabs. Rebuilding the same object by hand invites drift, so save working objects as prefabs early.
  • ECS too soon. Moving to ECS before profiling shows a need adds work for no gain, so prove the bottleneck first.

Start with small components, connect them loosely, and let the parts grow only where the game actually needs them.

Key takeaways

  • A GameObject gets its behaviour from attached components, not from a class tree
  • Composition lets you build new characters by swapping parts instead of writing subclasses
  • Components should talk through events or shared data, not tight direct references
  • Prefabs turn a finished GameObject into a reusable template for the whole game
  • Unity's ECS suits thousands of similar objects; most small games do fine without it

Frequently asked questions

What is a component in Unity?

A component is a part attached to a GameObject that adds one behaviour, such as movement, rendering or sound. Every GameObject has a Transform component, and you add others, including your own scripts, to give the object its behaviour.

What is the difference between a GameObject and a prefab?

A GameObject is a single object in a scene. A prefab is a saved template of a GameObject with all its components, so you can spawn many copies that share the same setup and update them all by editing the prefab.

Should I use inheritance or components in Unity?

Use components. Inheritance forces game objects into a family tree that rarely fits, while components let you combine behaviours freely. A flying enemy and a swimming enemy can share a health component without sharing a base class.

When should I use Unity ECS instead of GameObjects?

Use ECS when profiling shows the classic GameObject model is too slow, usually with thousands of similar objects such as crowds or particles. For most small games, GameObjects with MonoBehaviour scripts are easier to build and debug.

What is a ScriptableObject used for?

A ScriptableObject is an asset that holds data or events outside any scene, so many objects can share it. Teams use it for player stats, game settings and event channels that let components talk without direct references.

How small should a component be?

Small enough to do one job well, usually under 200 lines. If a component handles movement, health and scoring, split it. If splitting a simple behaviour into four parts adds confusion, keep it as one.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a game in Unity and want the architecture to stay clean? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Custom Software Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call