Overview
In Unity's world, everything that exists in a scene is called a GameObject. And Components are what give those "things" movement, functionality, and appearance. This "GameObject and Component" relationship is the cornerstone of Unity's design philosophy—the most important concept to understand.
Think of a GameObject as an "empty container" or "model kit body," and Components as the "engines" and "decorative parts" you attach to it. By combining various parts (Components), a single GameObject can fulfill diverse roles. This article explains these two foundational elements and how they work together.
Core Concepts
GameObject
A GameObject is the fundamental building block of Unity scenes. Player characters, enemies, trees, walls, lights, cameras—everything visible and invisible is a GameObject.
However, a freshly created "empty" GameObject can do almost nothing on its own. The only thing it has is the Transform component, the most basic component that manages the object's "Position," "Rotation," and "Scale."
Think of a GameObject as the "foundation" or "container" to which you'll add various functionality.
Component
A Component is a "part" that adds specific functionality to a GameObject. Unity provides numerous built-in components:
- Mesh Renderer: Draws the object's shape (mesh) on screen.
- Rigidbody: Gives the object physical behavior (gravity, collisions).
- Collider: Defines the object's collision detection shape.
- Audio Source: Plays sound from the object.
- Light: Makes the object a light source.
- Camera: Makes the object a camera that renders the game world.
Most importantly, custom C# scripts are also treated as components. You implement unique logic and behavior by creating script components and adding them to GameObjects.
Implementation
Let's work with GameObjects and Components in the Unity Editor.
1. Creating GameObjects
Right-click in the Hierarchy window and select Create Empty to create an empty GameObject with only a Transform component. This is the purest "container."
Alternatively, selecting 3D Object > Cube creates a cube GameObject. Checking this Cube in the Inspector window reveals these components automatically added beyond Transform:
- Mesh Filter: Specifies the display shape (mesh) as "Cube."
- Mesh Renderer: Draws the shape specified by Mesh Filter.
- Box Collider: Cube-shaped collision detection.
2. Adding Components
Components can be freely added to give GameObjects new functionality. Let's add physics to our Cube:
- Select the Cube in Hierarchy.
- Click the
Add Componentbutton at the bottom of the Inspector. - Type "Rigidbody" in the search box and select
Rigidbody.
That's it—the Rigidbody component is now attached to the Cube. Play the game and the Cube will fall due to gravity. This is the moment functionality is added through a component.
3. Adding Script Components
Now let's create a C# script to add custom movement:
- Right-click in the Project window and select
Create > C# Script. Rename it "SimpleMover." - Double-click the script to open it and write:
using UnityEngine;
// Inheriting MonoBehaviour makes this class behave as a Component
public class SimpleMover : MonoBehaviour
{
public float speed = 5f;
// Update is called every frame
void Update()
{
// Manipulate the Transform of the GameObject this script is attached to
// Move right at 'speed' units per second
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
- Save the code and drag the
SimpleMoverscript from Project onto the Cube in Hierarchy.
The SimpleMover component is now attached to the Cube. Play the game and the Cube falls with gravity while moving right. Custom scripts work as components that control GameObject behavior.
Best Practices
- Component-Oriented Design: Unity follows "component-oriented" thinking. Rather than one massive class managing everything, create small, focused components—"movement feature," "attack feature," "HP management"—and combine them to build GameObjects.
- Don't Overload Single GameObjects: If a "Player" GameObject handles movement, attacks, inventory, and UI display, complexity grows quickly. Use child objects to distribute roles—"empty object for holding weapons," "empty object for spawning effects"—making management easier.
Summary
GameObjects and Components are the foundation of Unity development. Understanding their relationship is the first step to mastering Unity.
- GameObject: The "container" that serves as the foundation for everything in scenes.
- Component: The "parts" that give GameObjects functionality and behavior. Custom scripts are also components.
- Component-Oriented: Build complex objects by combining small, focused components.
Start by experimenting in the Editor—add various components to GameObjects and observe the changes.