Unity uses C# scripts to define behavior for GameObjects.
Scripts are added as components and run automatically by the engine.

Unity Scripts Basics

Unity scripts are written in C# and edited using external code editors such as Visual Studio, Visual Studio Code, or JetBrains Rider. The default script editor can be changed in Edit → Preferences → External Tools → External Script Editor, so that double-clicking a script in the Project window opens it in your preferred editor.

Scripts are C# files that inherit from MonoBehaviour.

A script becomes active when attached to an active GameObject.

Script file names should not contain spaces, and each word should start with a capital letter (PascalCase), for example: PlayerMovement, EnemyController, CameraFollow.

A MonoBehaviour script is a script that inherits from MonoBehaviour and can be attached to GameObjects, allowing it to receive Unity events such as Start() and Update().

A regular C# class does not inherit from MonoBehaviour and cannot be attached to GameObjects. It is typically used for data structures, helpers, or game logic that does not need to exist as a component in the scene.

How-to steps:

  • Right-click in Project → Create → Scripting and select the type of script you want to create.

  • Name the script.

  • Drag script onto a GameObject.

  • Open script in code editor.

Typical script structure:

using UnityEngine; // Provides access to Unity-specific classes and functions

// The class name 'Example' must match the filename (Example.cs) public class Example : MonoBehaviour {
  // Start is called once before the first frame update    void Start() {
  // Use this for initialization (e.g., setting starting variables)    }
  // Start is called once before the first frame update
    void Update() {   // Use this for frame-dependent logic (e.g., player movement or timers)    } }

Warning:

  • Script filename must match class name.

  • Compilation errors prevent Play Mode.

  • One GameObject can have many script.

Further Reading:
https://docs.unity3d.com/6000.3/Documentation/Manual/creating-scripts.html

Unity Lifecycle Methods

Unity automatically calls certain functions at specific times.

Common ones:

  • Awake() → when object is created

  • Start() → before first frame

  • Update() → every frame

  • FixedUpdate() → physics updates

  • OnEnable() / OnDisable()→ when script is enabled/disabled via code or inspector

Understanding execution order prevents bugs and timing issues.

Warning:

  • Physics code belongs in FixedUpdate.

  • Update runs faster on high FPS (Frames Per Second) machines.

  • Awake runs even if component disabled.

Further Reading:
https://docs.unity3d.com/Manual/execution-order.html

Movement & Rotation

Every GameObject in Unity has a Transform component.
The Transform stores three fundamental properties:

  • Position (where the object is)

  • Rotation (how it is oriented)

  • Scale (how big it is)

You can modify these values manually in the Inspector by selecting a GameObject and editing its Transform fields.

The same Transform can also be accessed and modified through scripts.
Inside a MonoBehaviour script, the variable transform automatically refers to the Transform of the GameObject the script is attached to.

How-to:

  • Select a GameObject.

  • In the Inspector, change Position, Rotation, or Scale.

  • Attach a script to the object.

  • Use transform.position, transform.rotation, or helper functions such as Translate() and Rotate().

Example:

  transform.Translate(0, 0, 1);
  transform.Rotate(0, 90, 0);

To move smoothly over time, multiply movement by Time.deltaTime.

Warning:

  • Direct Transform movement ignores physics.

  • For physics-based objects, Rigidbody movement is preferred.

  • Forgetting Time.deltaTime makes movement speed depend on frame rate.

Further Reading:
https://docs.unity3d.com/Manual/class-Transform.html
https://docs.unity3d.com/ScriptReference/Transform.html

Collisions & Triggers

Unity uses Colliders to define the physical shape of an object for detecting contact. Colliders come in different forms such as Box Collider, Sphere Collider, Capsule Collider, and Mesh Collider. Primitive colliders (box, sphere, capsule) are fast and recommended for most objects, while Mesh Colliders match the exact shape of a model but are more expensive to calculate. Collider is added to a GameObject as component via Inspector or code.

Two main interaction types exist:

  • Collision → physical contact and blocking

  • Trigger → overlap detection without blocking

Collision

A collision occurs when two objects with Colliders touch and at least one of them has a Rigidbody.

Used for:

  • Walls

  • Floors

  • Physical obstacles

  • Pushing objects

Required components:

  • Collider on both objects

  • Rigidbody on at least one object

The Collision Lifecycle
Unity provides three main methods for physical collisions. They trigger at different stages of the contact:

  • OnCollisionEnter(Collision collision): Fires at the exact moment the objects touch.

  • OnCollisionStay(Collision collision): Fires every frame the objects remain in contact.

  • OnCollisionExit(Collision collision): Fires the moment the objects stop touching.

The Collision collision parameter is an object that contains detailed data about the impact, such as the point of contact, the speed of the hit, and a reference to the specific GameObject that was struck.

In Unity, Collision Detection modes determine how the physics engine tracks moving objects to prevent them from "tunnelling" (teleporting through walls) at high speeds. Discrete is the default setting that checks for collisions at specific time intervals (frames). Continuous is more intensive mode that "sweeps" the object’s path to ensure it doesn't pass through static geometry. Check video for the visual:

 

 

Tip
Only use Continuous modes on the specific objects that actually need it (like a projectile). Setting every object in your scene to Continuous will significantly tank your game's performance!

Trigger
In Unity, Trigger methods are used when you want to detect when one object overlaps another or something enters or leaves an area, without causing a physical "bounce."

Used for:

  • Pickups

  • Checkpoints

  • Damage zones

  • Detection areas

Required components:

  • Collider with Is Trigger enabled

  • Rigidbody on at least one object

The Trigger Lifecycle
Similar to collisions, triggers have three distinct phases:

  • OnTriggerEnter(Collider other): Triggered the moment the object enters the zone (e.g., picking up a coin).

  • OnTriggerStay(Collider other): Triggered every frame the object remains inside (e.g., standing in a zone that heals you over time)

  • OnTriggerExit(Collider other): Triggered when the object leaves the zone (e.g., a door closing after you walk through).

Further Reading:
https://docs.unity3d.com/Manual/CollidersOverview.html
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
https://docs.unity3d.com/Manual/collider-interactions-other-events.html