Game Development
What is a GameObject?
Learn the role of GameObjects in Unity and the component system.
What is a GameObject?
In Unity, every entity in a scene is a GameObject. Characters, lights, cameras, and floor objects all exist in the scene as GameObjects. On its own, a GameObject is just an empty container (entity) with no intrinsic behavior.
// Creating an empty GameObject with Unity C#
GameObject player = new GameObject("Player");
What gives a GameObject life, appearance, and behavior are the components attached to it.
GameObject and the Transform relationship
Every created GameObject invariably contains at least one Transform component. This component cannot be removed because every object needs a position, rotation, and scale in the scene.
GameObject item = new GameObject("Coin");
Console.WriteLine(item.name); // Coin
This guarantees that every scene object represents a point in space, and other components build upon this foundation.
Exercise Print GameObject name
In a console program representing a GameObject structure, assign "Hero" to the name variable and print the object name using Console.WriteLine.
Show example solution
using System;
class Program
{
static void Main()
{
string name = "Hero";
Console.WriteLine("GameObject: " + name);
}
}
Test yourself
-
What is the most accurate definition of a GameObject in Unity?
- A database that only stores the file extension of 3D models.
- The hardware layer that directly controls the game's graphics driver.
- An engine tool that only plays audio effects on screen.
- The fundamental container that holds components and represents scene entities.
Show answer
The fundamental container that holds components and represents scene entities. — GameObject is the foundation of Unity's entity model; it gains functionality through attached components.
-
Instantiate a GameObject
GameObject enemy = ____ GameObject("Goblin");
Show answer
GameObject enemy = new GameObject("Goblin");
-
GameObject core terms
- GameObject
- Base container for all scene entities
- Component
- Adds behavior and properties to objects
- Entity
- Abstract object model in the game world
- Transform
- Mandatory position component per object
Next lesson: Components