Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Game Engines  ›  Lesson

Unity: GameObjects, Components and C#

Unity 3D 12 minUnity builds scenes from GameObjects that carry components, calls C# Update every frame and FixedUpdate on the fixed physics step, and stores position, rotation and scale in each object's Transform.

Unity: GameObjects, Components and C#

Open Harbour Hopper in Unity and you won't find a while (running) anywhere. A crate is an object in a scene with a few parts attached, and your code is a small C# class that Unity calls at the right moments. Everything from the earlier sections is still there; it just goes by Unity names.

The rule

GameObjects hold components; Update runs per frame, FixedUpdate per physics step.

A GameObject is an empty container in a scene. Components give it abilities:

  • Transform: every GameObject has one. It stores position (a Vector3), rotation (a Quaternion) and localScale: the scale, rotate and translate transform from Section 3. The inspector shows the rotation as Euler angles for people to edit.
  • Rigidbody: hands the object to the physics engine, with a mass and gravity.
  • Collider: the object's physical shape, such as a box.
  • Your scripts: C# classes that derive from MonoBehaviour.

A configured GameObject can be saved as a prefab, a template you can place in scenes or spawn from code as many times as you like.

Unity calls Update once per rendered frame, with Time.deltaTime holding that frame's delta time, and FixedUpdate once per physics step. By default the physics step is 0.02 s, 50 times a second: the accumulator from Section 1, run for you.

using UnityEngine;

public class BoatSail : MonoBehaviour
{
    public float speed = 3f;   // metres per second

    void Update()
    {
        transform.position += Vector3.right * speed * Time.deltaTime;
    }
}

Unity's world has y pointing up, and one unit is treated as one metre, so default gravity is about 9.81 m/s² downward. A Physics Material on a collider holds friction values and bounciness, which is Unity's name for restitution.

The shortcut

Translate each course idea into its Unity name: loop → Update; fixed step → FixedUpdate; vector → Vector3; quaternion → Quaternion; affine transform → Transform; restitution → bounciness. Plain movement goes in Update with Time.deltaTime; forces on a Rigidbody go in FixedUpdate.

Building the crate prefabStep 1 of 5
  1. Create the object

    A GameObject named Crate. It already has a Transform.

The trap is moving a Rigidbody by writing transform.position every frame. That fights the physics engine and makes collisions misbehave. Apply forces, or set the velocity, on the Rigidbody in FixedUpdate instead.

Drills

Answer fast — trust the rule.

quick drill

Which Unity method should apply the drone's steady thrust force to its Rigidbody?

quick drill

Which component does every Unity GameObject always have?

quick drill

By default, how often does Unity call FixedUpdate?

quick drill

What type is transform.rotation in Unity?

quick drill

Which Physics Material setting is Unity's version of restitution?

Remember

  • GameObjects are containers; components such as Transform, Rigidbody, Collider and your scripts give them abilities.
  • Update with Time.deltaTime runs per frame; FixedUpdate runs per 0.02 s physics step and is where forces go.
  • Don't drag a Rigidbody around through its Transform; let physics move it.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe crate in Unity

Describe how you would set up Harbour Hopper's crate in Unity so that it falls, slides and bounces as in the Dynamics section. Name the components and settings, and explain which script method should apply the drone's lifting force and why.