Menu
CoddyTech

Small Components

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 1 of 50.

A GameObject is a list of components, and each script you write is one more of them. Unity games are built by giving each script one job and putting several scripts on an object. A player's moving, shooting and hit points could all live in one PlayerController, but as three components, Mover, Shooter and Health, each one can go on other objects too:

public class Health : MonoBehaviour
{
    public int hp = 3;

    public void TakeDamage(int amount)
    {
        hp -= amount;
        if (hp <= 0)
        {
            Destroy(gameObject);
        }
    }
}
  • Player: Mover, Shooter, Health
  • Turret: Shooter, Health
  • Crate: Health

With plain C# classes you might build a tree instead: Character, then ShootingCharacter : Character, then Player : ShootingCharacter. A turret shoots but never walks and a crate only breaks, so every new kind of object means reshaping the tree. With components, a new kind of object is a new mix of scripts that already exist, put together in the Inspector, and each script can have its own values there: a slow Mover on one object, a fast one on another.

Components on one object share its Transform, so each should change only the part it owns. A Mover that adds to x and z with transform.position += leaves y alone, so a Jumper on the same object can still change the height. A Mover that set the whole position to new Vector3(x, 0.5f, z) would put y back every frame and cancel the jump.

challenge icon

Challenge

Easy

Boat.cs does three jobs at once: the arrows (the Move action) steer the boat, the boat bobs on the waves, and its lamp blinks. The duck needs the bobbing too, and the buoy needs the bobbing and the blinking, so the scene already gives them Bobber and Blinker components, and the boat has all three. Split Boat.cs into them:

  • Bobber: each object bobs around the height it starts at, height units up and down, at its own speed (the Inspector sets both). Bobber changes only the y of the position, so the boat can still steer.
  • Blinker: it switches lamp (a Renderer) on and off every interval seconds.
  • Boat keeps only the steering: delete the bobbing and the blinking, with their fields.

Press Play and steer the boat around, then Check.

Try it yourself

using UnityEngine;
using UnityEngine.InputSystem;

// One script, three jobs: steering, bobbing and blinking.
public class Boat : MonoBehaviour
{
    public float speed = 3f;
    public float bobHeight = 0.1f;
    public float bobSpeed = 2f;
    public Renderer lamp;
    public float blinkInterval = 0.5f;

    private InputAction moveAction;
    private float startY;
    private float blinkTimer;

    void Start()
    {
        moveAction = InputSystem.actions.FindAction("Move");
        startY = transform.position.y;
    }

    void Update()
    {
        // steering
        Vector2 input = moveAction.ReadValue<Vector2>();
        transform.position += new Vector3(input.x, 0f, input.y) * speed * Time.deltaTime;

        // bobbing
        Vector3 p = transform.position;
        p.y = startY + Mathf.Sin(Time.time * bobSpeed) * bobHeight;
        transform.position = p;

        // blinking
        blinkTimer += Time.deltaTime;
        if (blinkTimer >= blinkInterval)
        {
            blinkTimer -= blinkInterval;
            lamp.enabled = !lamp.enabled;
        }
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Architecture in Unity