# Unity Mental Model: GameObject + Components

Unity development is built around one core idea:

```text
GameObject = container
Component  = behavior or data attached to that container
```

A GameObject by itself does almost nothing. It becomes useful because of the components attached to it.

## The Core Model

Think of a GameObject as an empty holder in the scene or prefab.

Examples:

```text
Rika
Monster
Witch
fire_camp
Rika_Bullet
Main Camera
Global Light 2D
```

Each one is a GameObject. What makes them different is their components.

For example:

```text
Rika
├── Transform
├── SpriteRenderer
├── Animator
├── Rigidbody2D
├── CapsuleCollider2D
├── RikaPlayerController
├── PlayerShooter
├── PlayerRespawn
└── PlayerFuel
```

The GameObject is named `Rika`, but the components make it visible, animated, physical, controllable, able to shoot, able to take damage, and able to respawn.

## Every GameObject Has A Transform

Every GameObject has a `Transform`.

The Transform stores:

```text
Position
Rotation
Scale
Parent/child relationship
```

Examples:

```text
Rika position decides where the player is in the level.
FirePoint position decides where Rika's bullet appears.
Camera position decides what part of the level is visible.
AttackHitbox position decides where enemy attack damage happens.
```

Do not delete a Transform. It is required by Unity.

## Components Add Capabilities

Common component categories:

| Component type | What it does |
| --- | --- |
| `Transform` | Position, rotation, scale, hierarchy |
| `SpriteRenderer` | Draws a sprite |
| `Animator` | Plays animations |
| `Rigidbody2D` | Adds 2D physics movement |
| `Collider2D` | Defines physical or trigger shape |
| Script component | Adds custom gameplay logic |
| `Camera` | Renders the scene |
| `Light2D` | Lights sprites in URP 2D |

## Example: Rika

Rika is not one script. Rika is a GameObject with several components.

Important components:

```text
Transform
SpriteRenderer
Animator
Rigidbody2D
CapsuleCollider2D
RikaPlayerController
PlayerShooter
PlayerFuel
PlayerRespawn
```

Responsibilities:

```text
SpriteRenderer       = shows Rika's current sprite
Animator             = controls idle, run, jump, dash, attack, hit animations
Rigidbody2D          = lets physics move Rika
CapsuleCollider2D    = lets Rika collide with ground and enemies
RikaPlayerController = handles movement, jump, dash, input, hit response
PlayerShooter        = spawns bullets
PlayerFuel           = health/fuel value
PlayerRespawn        = death fall and checkpoint respawn
```

This is the Unity pattern: one object, many focused components.

## Example: Monster / Witch

Enemies also use the component model.

Typical enemy setup:

```text
Monster or Witch
├── Transform
├── SpriteRenderer
├── Animator
├── CapsuleCollider2D
├── Rigidbody2D
├── EnemyHealth
├── MonsterAnimator
├── MonsterContactAttack
├── MonsterHealthBarGUI
└── AttackHitbox
```

The object becomes an enemy because these components work together:

```text
EnemyHealth           = stores HP and handles death
MonsterAnimator       = sends animation triggers
MonsterContactAttack  = patrol, chase, attack timing
MonsterAttackHitbox   = damages Rika during attack frames
MonsterHealthBarGUI   = draws HP bar above enemy
```

Witch also has:

```text
WizardShooter
FirePoint
Wizard_bullet prefab reference
```

That lets Witch shoot projectiles during attack animation events.

## Example: Projectile

A projectile prefab is also just a GameObject with components.

Player bullet:

```text
Rika_Bullet
├── Transform
├── SpriteRenderer
├── Rigidbody2D
├── Collider2D
└── Bullet
```

Enemy bullet:

```text
Wizard_bullet
├── Transform
├── SpriteRenderer
├── Rigidbody2D
├── Collider2D
└── EnemyProjectile
```

Responsibilities:

```text
SpriteRenderer = visible bullet sprite
Rigidbody2D    = bullet velocity
Collider2D     = hit detection
Bullet script  = player bullet damage/lifetime behavior
EnemyProjectile = enemy bullet damage/lifetime behavior
```

The bullet is not permanently attached to Rika or Witch. It is spawned from a prefab when an attack happens.

## Example: Fire Camp Checkpoint

The fire camp is a reusable environment prefab.

Current pattern:

```text
fire_camp
├── Transform
├── SpriteRenderer
├── Animator
├── CircleCollider2D
├── Checkpoint
└── RestCheckpoint
```

Responsibilities:

```text
SpriteRenderer  = draws the camp
Animator        = plays fire animation
CircleCollider2D = detects when Rika is close
Checkpoint      = stores respawn point and saved fuel
RestCheckpoint  = shows "Press E to rest" and refills fuel
```

Again, the GameObject is only the container. The components define what it does.

## Scene Object vs Prefab

There are two common forms of GameObjects:

```text
Prefab asset
Scene instance
```

A prefab asset lives in the Project window.

Example:

```text
Assets/Prefabs/Characters/Rika.prefab
Assets/Prefabs/Characters/Monster.prefab
Assets/Prefabs/Environment/fire_camp.prefab
```

A scene instance is a placed copy inside a scene hierarchy.

Example:

```text
SampleScene
├── Rika
├── Monster
├── Witch
└── fire_camp
```

Rule:

```text
Reusable setup belongs on the prefab.
Level-specific placement belongs in the scene.
```

If you change a prefab asset, all normal instances can inherit the change.

If you change only a scene instance, that change may apply only to that one placed object.

## Parent And Child GameObjects

GameObjects can be nested.

Example:

```text
Rika
└── FirePoint
```

`FirePoint` is a child GameObject used as a position marker. It does not need to draw anything. It only tells `PlayerShooter` where bullets should spawn.

Example:

```text
Witch
├── AttackHitbox
└── FirePoint
```

`AttackHitbox` is a child object with a trigger collider. It is enabled only during attack frames.

`FirePoint` is a child position marker for projectile spawning.

Use child GameObjects when one object needs helper positions or separate colliders.

## Inspector References

Many script components have fields that must be assigned in the Inspector.

Example from `PlayerShooter`:

```text
Bullet Prefab
Fire Point
Visual Sprite Renderer
```

This means:

```text
PlayerShooter does not magically know which bullet to spawn.
You must drag the bullet prefab into the Bullet Prefab field.
You must drag the FirePoint transform into the Fire Point field.
```

If a reference is missing, the script may not work.

Common missing-reference symptoms:

```text
Nothing spawns
Animation does not play
NullReferenceException
Warning in Console
Object appears at wrong position
```

## Components Communicate With Each Other

Scripts usually find or reference other components.

Examples:

```csharp
GetComponent<Rigidbody2D>()
GetComponent<Animator>()
GetComponent<PlayerFuel>()
GetComponentInChildren<MonsterAttackHitbox>()
```

This means:

```text
The script expects another component to exist on the same GameObject or child object.
```

Example:

`RikaPlayerController` expects:

```text
Rigidbody2D
Animator
SpriteRenderer
```

That is why it uses Unity attributes like:

```csharp
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(SpriteRenderer))]
```

This tells Unity that the script depends on those components.

## Update, FixedUpdate, And Events

Unity calls special methods automatically.

Common methods:

| Method | Use |
| --- | --- |
| `Awake()` | Get component references |
| `Start()` | Initialize after objects exist |
| `Update()` | Read input, timers, non-physics logic |
| `FixedUpdate()` | Physics movement |
| `OnTriggerEnter2D()` | Trigger collider entered |
| `OnCollisionEnter2D()` | Solid collision happened |
| `OnGUI()` | Simple immediate UI drawing |

Examples in this project:

```text
RikaPlayerController.Update()
→ reads movement, jump, dash, attack input

RikaPlayerController.FixedUpdate()
→ applies horizontal Rigidbody2D velocity

MonsterContactAttack.FixedUpdate()
→ patrols, chases, and moves enemies

RestCheckpoint.OnTriggerEnter2D()
→ detects Rika near fire camp

Bullet.OnTriggerEnter2D()
→ detects what the bullet hit
```

## Collision Mental Model

For 2D physics, usually you need:

```text
Rigidbody2D
Collider2D
```

Common collider modes:

```text
Solid collider
Trigger collider
```

Solid collider:

```text
Blocks movement.
Used for Rika body, enemy body, ground.
```

Trigger collider:

```text
Detects overlap without blocking.
Used for bullets, attack hitboxes, fire camp interaction range.
```

Example:

```text
Monster body collider = solid
Monster attack hitbox = trigger
```

This lets the monster stand on platforms while its attack hitbox can temporarily detect Rika during attack frames.

## Animation Mental Model

Animation in Unity usually has three pieces:

```text
Sprite frames
Animation clip
Animator controller
```

The script does not usually choose individual sprites. It tells the Animator what state to play.

Example:

```csharp
animator.SetTrigger("Attack");
animator.SetTrigger("Death");
animator.SetBool("IsGrounded", true);
```

Then the Animator Controller decides which animation clip plays.

Animation events can call script methods.

Example:

```text
Monster_Attack animation
→ calls EnableAttackHitbox()
→ later calls DisableAttackHitbox()
```

That means damage is synchronized with the attack frames.

## Practical Rules For Teammates

### Do Not Add Logic To Random Scene Objects

If behavior should be reusable, put it on a prefab.

Good:

```text
Add RestCheckpoint to fire_camp.prefab
```

Risky:

```text
Add RestCheckpoint only to one fire_camp scene instance
```

### Do Not Duplicate Scripts For Every Enemy Type Too Early

If Monster and Witch share movement, use the same component.

Current example:

```text
MonsterContactAttack
```

It is used for enemy patrol/chase/attack timing.

Only create a new script when the behavior is truly different.

### Prefer Small Components

Good component split:

```text
EnemyHealth
MonsterAnimator
MonsterContactAttack
WizardShooter
MonsterAttackHitbox
```

Bad direction:

```text
One giant EnemyEverything.cs
```

Small components are easier to test and debug.

### Check The Inspector Before Editing Code

Many problems are caused by setup, not code.

Before changing scripts, check:

```text
Is the component attached?
Is the prefab reference assigned?
Is the collider trigger setting correct?
Is the Rigidbody2D body type correct?
Is the sorting layer correct?
Is the Animator Controller assigned?
Is the animation parameter spelled correctly?
```

## Common Mistakes

### Mistake: "I added a script file, but nothing happens"

Creating a script file is not enough.

You must attach it as a component to a GameObject or prefab.

### Mistake: "I changed a scene object, but other copies did not change"

You probably changed a scene instance instead of the prefab asset.

Open the prefab and apply the change there if all copies should use it.

### Mistake: "The object exists but is invisible"

Check:

```text
SpriteRenderer enabled?
Sprite assigned?
Sorting Layer correct?
Order in Layer correct?
Scale not zero?
Camera can see it?
2D Light applies to its sorting layer?
```

### Mistake: "The object does not collide"

Check:

```text
Collider2D exists?
Rigidbody2D exists where needed?
Is Trigger set correctly?
Layer collision matrix allows it?
Script is using OnTriggerEnter2D vs OnCollisionEnter2D correctly?
```

### Mistake: "NullReferenceException"

Usually means:

```text
A script expected a reference, but the reference is missing.
```

Check the Inspector fields first.

## Summary

The Unity mental model is:

```text
GameObjects are containers.
Components give GameObjects abilities.
Prefabs save reusable GameObject setups.
Scenes place prefab instances into levels.
Scripts are custom components.
Inspector references connect components and assets.
```

When debugging, always ask:

```text
Which GameObject is involved?
Which component owns this behavior?
Which prefab or scene instance has the actual setup?
Which reference or component might be missing?
```

This mindset will make the rest of the project easier to understand.
