Lesson 1 of 9 · 24m
GameObjects, Components, and Composition
Learn why Unity favors small composable components over deep inheritance, and how to structure objects without creating dependency spaghetti.
The fastest way to write unmaintainable Unity code is to treat MonoBehaviour like a junk drawer. I have inherited projects where a 4,000-line PlayerController handled movement, inventory, dialogue, achievements, save data, audio, and camera shake. It worked until a designer asked whether enemies could pick up items too. In Unity, behavior belongs in small composable pieces.
A GameObject is a container, not a design pattern
A GameObject is mostly a name, a transform, and a bag of components. The interesting behavior comes from composition:
Player
├── Transform
├── Rigidbody2D
├── CapsuleCollider2D
├── SpriteRenderer
├── PlayerMovement
├── Health
└── DamageFlashNone of those components needs to know everything about the others. PlayerMovement moves the body. Health owns hit points. DamageFlash reacts when damage occurs. If an enemy also needs health, reuse Health; do not subclass the player.
Prefer dependency direction over scene archaeology
New Unity programmers often write this:
var player = GameObject.Find("Player").GetComponent<PlayerInventory>();That line is three failures in a trench coat: expensive lookup, fragile naming, and a hidden dependency. Make dependencies explicit:
public class DamageOnTouch : MonoBehaviour
{
[SerializeField] private int damage = 1;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.TryGetComponent(out Health health))
health.TakeDamage(damage);
}
}The collider relationship is visible in the scene. The dependency goes through a narrow interface. There is no global scavenger hunt.
Serialize references at edit time
Use [SerializeField] for private fields that designers need to tune. It keeps the public API honest while exposing data in the Inspector:
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private LayerMask groundMask;
[SerializeField] private Transform groundCheck;Public fields serialize too, but they invite unrelated scripts to mutate state. Serialize narrowly; expose operations, not variables.
Composition is not the absence of architecture
Tiny scripts can still become spaghetti if every component reaches sideways into every other component. Use events, interfaces, or a deliberate owner object for coordination. A good rule: leaf components may depend inward on shared primitives, but shared primitives should not know who uses them.
Field notes
- Missing references usually mean a scene object pointed at a prefab asset, or the other way around. Understand which object owns the reference before relinking blindly.
GetComponentis cheap enough in setup and expensive enough in hot loops; cache references duringAwake.- Do not put gameplay logic in
Updatejust because it is convenient. Some behavior belongs in collision callbacks, input actions, or fixed simulation. - Inheritance is useful for editor tooling and shared framework code. It is usually the wrong tool for game entities.
Was this lesson helpful?