Lifecycle methods
Find when a script runs, see why Unity events have their own Lifecycle section, and build a spike that sends the player back to the start.
On this page
Lifecycle puts the moments Unity runs your script at the top of the left rail: setting up an object, updating it each frame, or reacting when something touches it. Start here to find what makes an object act, without searching through all its other methods.
New to scripts? A method is a named task, with its own canvas. Unity knows certain method names, such as Start, Update, and OnTriggerEnter2D, and calls them at the matching moment. These are Unity’s event functions, also called callbacks or messages. Round Trip groups them under Lifecycle.
- Looking up a moment? Pick the moment.
- Ready to edit? Open or add a lifecycle method.
- Learning by doing? Make a spike in the Platformer sample: touch it and the player goes back to the start, with no C# typing. Your first class is a gentler place to begin if you have not built a visual script yet.
- Already know Unity? Jump to the C# details or see the generated script.
Why Lifecycle has its own section
Section titled “Why Lifecycle has its own section”When you open an unfamiliar script, the most important methods to find first are usually its setup and per-frame work, along with its reactions to game events. Round Trip lifts these out of the general Methods list into Lifecycle, above it, so you do not have to hunt through helper methods to find the starting points.
| Left-rail section | The question it answers | Platformer examples |
|---|---|---|
| Lifecycle | When does this script react? | Awake sets the player up; OnTriggerEnter2D reacts to entering a coin’s area. |
| Methods | What work can something ask this script to do? | CollectCoin collects a coin; RespawnAtStart sends the player home. |
Lifecycle methods are still methods in the same C# class. The separate section changes where you find them, not how they run. Recognized callbacks already in your script appear here automatically, rather than being duplicated under Methods. Click one to read or edit its canvas just like any other method.
A method under Methods needs a caller: another method, another script, or an event connected in Unity’s Inspector. Those Inspector connections are another way into a script, so begin with Lifecycle and follow the calls from there.
Unity’s own documentation is the authority on what each callback means and when it fires. Start with its introduction to event functions; the Messages list in the MonoBehaviour scripting reference names every callback a script can receive, each linked to its own page. Each callback name in Pick the moment links to its page there too.
Pick the moment
Section titled “Pick the moment”Choose the moment that matches what you want to happen. These common callbacks cover everyday behaviors in the Platformer sample. The names in the middle column are the rows to pick in Add lifecycle method.
| When should it happen? | Pick | In the Platformer sample |
|---|---|---|
| Once, as the object appears. Use it to find the object’s own parts and remember starting values. | Awake | PlayerController finds the player’s body and collider, and remembers where the player started. |
| Once, before the enabled component’s first frame update. Use it for setup that needs other scene objects’ Awake work to be finished. | Start | Build it visually starts from a component that uses it to announce a coin when you press Play. |
| Every frame, many times a second. Use it to read keys or check rules and change what the player sees. | Update | PlayerController respawns a player who falls below the level. After the level is finished, Door restarts it when Return is pressed. Your first class uses it to move a square with the arrow keys. |
| On the steady physics clock, 50 times a second unless the project changes it. Use it to move anything that has a Rigidbody 2D or Rigidbody. | FixedUpdate | PlayerMotor moves and jumps the player. EnemyPatrol walks the enemy and turns it at the edges of its patrol. |
| When something enters an area it can pass through, such as a coin, a door, a checkpoint or your spike. | OnTriggerEnter2D | PlayerController collects the coin it touched. Door finishes the level when the player stands in its doorway. |
| When two solid objects bump into each other. | OnCollisionEnter2D | PlayerController stomps an enemy it lands on, and sends the player back to the start when they walk into one. PlayerMotor notes when the player is standing on the ground. |
| Every time the component is disabled or its object is switched off; also during destruction. | OnDisable | EnemyPatrol stops a defeated enemy from sliding. |
A few more you will meet:
- OnEnable runs when the component is enabled on an active object; LateUpdate runs each frame after Update, useful for a camera following a moving player.
- OnTriggerStay2D and OnTriggerExit2D react while something stays in a trigger area and when it leaves. OnCollisionStay2D and OnCollisionExit2D do the same for solid contact.
- OnDestroy handles cleanup when a previously active object is destroyed. Reset runs in the editor when you first add the component or use its Reset command; EnemyPatrol uses it to set a short patrol around where you place the enemy.
The picker groups callbacks under CORE, PHYSICS, FRAME, APP, INIT, and LOAD, and adds INPUT sections for player controls. Search by name when you already know the event you want.
Player controls
Section titled “Player controls”When the player presses a button, Unity’s Input System can call a method named On plus the control’s name: OnJump, OnMove, OnAttack. A Player Input component set to Send Messages makes those calls, using the actions in your project’s actions file. The picker reads them and lists them in three sections:
- INPUT · PLAYER and INPUT · UI: one row for each action in those maps of the actions file, such as OnMove and OnNavigate. Picking one writes
On…(InputValue value)with a first line that reads the value: a press check for a button,value.Get<Vector2>()for a direction,value.Get<float>()for an amount. - INPUT · DEVICES: OnDeviceLost, OnDeviceRegained and OnControlsChanged, which the Player Input sends when a gamepad is unplugged, plugged back in, or the player switches between keyboard and gamepad.
- New player control… opens a window that adds a new action with common keys and gamepad buttons, puts a Player Input on the player, saves the scene and writes the method in one step. Round Trip never points the Player Input at your project-wide actions file (Unity warns against that): it copies that file once to
Assets/PlayerControls.inputactions, uses the copy for this and every later control, and leaves the original untouched. Without the Input System package, the row opens Package Manager instead.
Your first class walks through the whole thing with a Jump button.
2D or 3D
Section titled “2D or 3D”A name that ends in 2D belongs to Unity’s 2D physics: Rigidbody 2D, Box Collider 2D, Circle Collider 2D and the other 2D colliders. The same name without 2D belongs to 3D physics: Rigidbody, Box Collider, Sphere Collider. They never mix. The Platformer is a 2D game, so your spike needed OnTriggerEnter2D; plain OnTriggerEnter would never have run.
Trigger or collision
Section titled “Trigger or collision”Both need a collider on each object, and at least one of the two objects needs a rigidbody. The player has one.
- A trigger is an area other objects pass through. Tick Is Trigger on the collider, and Unity raises the OnTrigger moments. Coins, doors, checkpoints and danger zones are triggers. Unity’s trigger collider setup covers the details.
- A collision is between two solid objects that push against each other. Leave Is Trigger unticked, and Unity raises the OnCollision moments. Floors and walls are solid, and so are enemies you can stand on.
Which pairs of colliders raise which moments depends on the kinds involved; Unity’s interaction between collider types has the full table.
The order Unity runs them
Section titled “The order Unity runs them”For a component on an active object, the usual starting sequence is Awake, OnEnable, then Start before its first update. Start runs once; OnEnable runs again when the component is enabled again.
While the component is enabled, Update and LateUpdate run each frame. FixedUpdate follows a separate physics clock: there can be zero, one, or several physics ticks in a rendered frame. Switching a component off calls OnDisable, not OnDestroy; destruction is a separate event.
The rail and picker use a configured order to make callbacks easy to scan. Their list order does not schedule your game, or guarantee which object’s callback runs first. Unity controls execution; see its event function execution order for the full rules.
Open or add a lifecycle method
Section titled “Open or add a lifecycle method”Open PlayerController and add a Lifecycle method
Open the class's rail
Open Tools > Round Trip > Graph view. Choose your class with the class name at the top left, or press Cmd/Ctrl+O.
8 stepsFull size ↗Filter and pick the moment
To open an existing callback, click its row under Lifecycle. To add one, press + on that section, type part of its name in Type to filter, then select it in Add lifecycle method.
Build and compile its canvas
Add the work to its canvas, or call one of your named Methods from it. Press Compile to save the canvas changes.
Make a spike
Section titled “Make a spike”You need the Platformer sample installed and its folder indexed, with the Platformer scene open.
1. Put a spike in the level
Section titled “1. Put a spike in the level”Build the spike's GameObject
Create the spike object
Choose GameObject > Create Empty, then press Add Component, type
Sprite Rendererand add it. In the Inspector, rename the objectSpike.
6 stepsFull size ↗Position and color it
Under Transform, set Position to
5,0.75,0and Scale to1,0.5,1. Under Sprite Renderer, set Color to red.Add a trigger collider
Press Add Component, type
Box Collider 2Dand add it. Tick Is Trigger.
2. Give the spike a script
Section titled “2. Give the spike a script”Create and attach the Spike script
Select the folder
In the Project window, select the sample’s
Platformer/Runtimefolder.Open New Class
Choose Assets > Round Trip > New class. A small window titled New class opens.
8 stepsFull size ↗Fill it out and create
Under NAME, type
Spike. Leave LOCATION as it is: Runtime, class, public. Under PARENT CLASS, click the MonoBehaviour card. Press create.Attach the script
Select the Spike object in the scene, press Add Component, type
Spikeand add it.
3. Choose the moment
Section titled “3. Choose the moment”Add OnTriggerEnter2D to Spike
Pick the class
Open Tools > Round Trip > Graph view. Click the class name at the top left of the window (or press Cmd/Ctrl+O) and pick Spike. The left rail then shows an empty Lifecycle section.
8 stepsFull size ↗Filter and add the event
Press + on the Lifecycle header. The Add lifecycle method list opens with the cursor in Type to filter. Type
trigger, then pick OnTriggerEnter2D(Collider2D) under PHYSICS: press Enter or click it.
4. Send the player back to the start
Section titled “4. Send the player back to the start”The player already knows how to go back to the start: its PlayerController script has a method called RespawnAtStart. The spike only needs to ask whatever touched it for its PlayerController, and call that.
Send the player back to the start
Drag from the other pin
Press on the other pin and drag onto empty canvas to its right, then let go. In the menu that opens, find GetComponent<T>() under Methods and click it.
4 stepsFull size ↗Choose PlayerController
A chooser titled GetComponent<T> asks which part of the object you want. Type
PlayerControllerinto Type name and pick RoundTrip.Platformer.Runtime.PlayerController under YOUR PROJECT.Drag to RespawnAtStart
Press on that result pin and drag onto empty canvas to the right. In the menu that opens, type
Respawnand, under Methods, click RespawnAtStart().
4 stepsFull size ↗Compile
Press Compile in the toolbar.
Press Play and walk right
Press Play and walk right. The moment you touch the spike, the player is back at the start.
5. Make it safe
Section titled “5. Make it safe”Right now the spike assumes that whatever touches it is the player. In the sample only the player can reach that spot, so it works. If an enemy or a falling object could touch the spike, it would have no PlayerController to respawn, and the game would log an error. The sample’s own scripts always check first, and you can add the same check with a branch:
Check for a player before respawning
Break the old link
Right-click the wire between GetComponent and RespawnAtStart and choose Break link.
Add a branch
Drag from the GetComponent node’s right Exec pin onto empty canvas. In the All actions menu, pick branch under FLOW CONTROL. A yellow BRANCH node lands, unwired; drag the same Exec pin to its Exec pin.
6 stepsFull size ↗Check for null
Right-click empty canvas below the branch, type
equals, and pick the Equals row with the = icon, not a method row with the ƒ call mark. If it lands on top of another node, drag it clear by its title. Wire the GetComponent result pin into its Left pin, click the small box beside Right, typenulland press Enter. Wire its Result into the branch’s Condition.
10 stepsFull size ↗Exit when missing
Drag from the branch’s true pin onto empty canvas and pick return under FLOW CONTROL. A magenta EXIT node lands; wire true into its Exec.
4 stepsFull size ↗Wire the safe path and compile
Wire the branch’s false pin into the RespawnAtStart node’s Exec. Press Compile.
4 stepsFull size ↗
The recipe behind it
Section titled “The recipe behind it”A useful way to organize a behavior is to separate the moment from the work:
- Name the moment. “When the player touches the spike” is OnTriggerEnter2D on the spike.
- Name what should happen. Often it is a method that already exists, like RespawnAtStart. When it is something new, give it a method of its own under Methods with a name that says what it does, built on its own canvas; New class and method shows how.
- Connect them on the moment’s canvas: check what happened if you need to, then call the method.
Keeping the work in named methods keeps each lifecycle canvas short, so it reads as “when this happens, do that”. Anyone who opens the class later sees every moment it reacts to under Lifecycle and what it does at each one by name. Call a neighbor builds the sample’s coin pickup this way.
Good to know
Section titled “Good to know”- The picker offers only the moments the class does not have yet, and once every one is added it says Every lifecycle method is implemented. The INPUT · PLAYER and INPUT · UI rows appear once a saved scene or prefab has a Player Input set to Send Messages or Broadcast Messages.
- An
On…(InputValue value)method that no saved Player Input calls shows a warning, … is never called: no PlayerInput beside this class sends messages for a “…” action. Unity calls these methods by name and says nothing when the name does not match, so a renamed action would otherwise leave the method silently unused. - A class placed on objects that have no collider or rigidbody does not get the touch moments offered, and the picker’s footer counts the hidden rows. A class that is not on any object yet, like Spike before you added it, is offered everything. Rows that cannot be added stay grayed out with the reason beside them.
- A lifecycle method’s name is locked. Hover it at the top of the right rail and the tooltip explains why: Unity finds the method by that exact name.
Already know Unity?
Section titled “Already know Unity?”- Same C# runtime: your
MonoBehaviourmessages remain ordinary C# declarations. Round Trip does not replace Unity’s event dispatch or move the method bodies to another script. - Recognition checks the method’s shape: the project catalog matches the name and accepted signature. An
Onprefix alone does not make a method a lifecycle callback. Project Settings > Round Trip contains Lifecycle recognition, shared by the team. - Use the two add controls deliberately: Lifecycle + chooses a recognized callback; Methods + creates a task you name yourself. Adding a callback writes the empty declaration immediately; editing its body waits for Compile.
What it writes
Section titled “What it writes”If you also work in C#, this is the whole of Spike.cs after the three-node version. Picking the moment wrote the empty method straight away; the nodes went in when you pressed Compile.
When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| Nothing happens when the player touches the spike | Select the Spike object. Does it have a Box Collider 2D with Is Trigger ticked, and the Spike component? Is the method named OnTriggerEnter2D, with the 2D? | Add what is missing. A solid collider raises OnCollision moments instead; a 3D moment never runs in a 2D game. |
| Something you built never runs | Look where it is listed in the left rail. A method under Methods runs only when something calls it. | Open the moment it should run at under Lifecycle, or add it with +, and call your method from there. |
| The Console shows a red error naming NullReferenceException when something touches the spike | Something without a PlayerController entered the trigger. | Add the check from Make it safe. |
| GetComponent is not in the menu | Did you drag from the other pin? The menu lists what the thing that touched can do. A right-click on empty canvas opens a different menu. | Drag from the other pin onto empty canvas, then look under methods. |
| Movement looks jittery or ignores walls | See whether something with a rigidbody is moved in Update. | Move it in FixedUpdate, and keep reading keys in Update. |
| The moment you want is not in the list | Read the list's footer for a hidden count, or its message if it is empty. | Add the matching collider or rigidbody to the object this class is on and press + again. If every moment is already added, there is nothing to add. |
| F2 does not rename the method | Hover the name at the top of the right rail for the lock message. | Keep Unity's name. Use the + on Methods for a method you name yourself. |
- Call a neighbor: build the coin pickup that the sample’s own OnTriggerEnter2D calls.
- Your first class: give a character a Jump button and build what it does.
- Guards: require values before a method runs.
- Branch: more on the yellow node you used to make the spike safe.
- Edits from outside



























