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(aVector3),rotation(aQuaternion) andlocalScale: 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.
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.
Which Unity method should apply the drone's steady thrust force to its Rigidbody?
Which component does every Unity GameObject always have?
By default, how often does Unity call FixedUpdate?
What type is transform.rotation in Unity?
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.
UpdatewithTime.deltaTimeruns per frame;FixedUpdateruns per 0.02 s physics step and is where forces go.- Don't drag a Rigidbody around through its Transform; let physics move it.
