Guards
Keep prerequisite checks and missing-reference handling in the right rail, so the canvas stays focused on what your game does.
On this page
The coin pickup needs a HUD before it can update the score display. A guard lets you say what should happen if that HUD is missing, without drawing a separate check and branch before the update.
A prerequisite is something that must be ready before work can continue. A missing reference means a value points to no object: for example, a HUD slot left empty in Unity’s Inspector. C# calls that value null.
Find your path
Section titled “Find your path”- Require a value for the method: pick it from a list and choose an outcome. No C# needed.
- Handle a missing value at one node: skip an optional call or choose a fallback.
- Set your project’s guard rules: choose shared defaults or add a custom C# pattern.
Why guards live in the right rail
Section titled “Why guards live in the right rail”Repeated missing-value checks can fill a diagram with comparisons, branches and early exits before you reach the game’s actual behavior. Round Trip lifts recognized checks into the right rail so the canvas is cleaner and easier to read. You can follow the coin pickup’s calls, then inspect its safety checks in one place.
The check still runs and still lives in the C# script. Lifting it changes how it is shown, not what the game does. Reading an existing script writes nothing; changing a guard becomes a draft until you press Compile.
| What you want to decide | Where to look |
|---|---|
| Can this method do its work? For example, is its HUD assigned? | Select the method in the left rail, then open GUARDS in the right rail. The header’s guarded values badge jumps to it. |
| What if this particular call or read has no object? For example, optional pickup audio. | Select the node, or click its pin’s shield marker, to see that node’s GUARDS card. |
| Which gameplay path should run? For example, take damage or block a hit. | Keep that decision on the canvas as a BRANCH. Checks that do not match a recognized guard also stay as nodes. |
Choose what happens when a value is missing
Section titled “Choose what happens when a value is missing”For a required object, Warn and skip the rest of this method is the usual starting point. It avoids using the missing object and explains the skipped work. The project’s available patterns determine which choices appear.
| Outcome | What it means |
|---|---|
| Stop with an error | Uses the project’s assertion/error pattern. Expand Advanced · show code to see the exact C#; assertion behavior depends on that pattern and Unity’s assertion settings. |
| Warn and skip the rest of this method | Runs the configured warning action, then returns before the rest of the method. A method that gives back a value also needs a return value. |
| Warn but carry on | Runs the configured warning action and continues. Use only when the remaining work is safe without the value; a warning alone cannot prevent a later missing-reference error. |
A yes-or-no variable or property can be a flag guard, such as “the level is configured”. Its heading reads If followed by the name and is false…. Flag guards are checked individually; they cannot join a combined missing-reference check.
Require a value for the method
Section titled “Require a value for the method”Require a value for the method
Open the method
Click the method you are building in the left rail. Scroll the right rail to GUARDS, or click the header’s guarded values badge. An empty card reads No required values yet.
Add a check and pick the value
Press Add a check. In Require a value, filter the list and tick the value the method needs, such as _hud. Press Require this value.
Choose the outcome
Expand the new check’s row if needed. Under If _hud isn’t set…, choose the If check option that ends in return. Edit Message if you want different words.
Compile
Press Compile in the toolbar.
Combine, customize, or remove checks
Section titled “Combine, customize, or remove checks”- Tick several values in the picker to Combine 2 values into one check. They share one outcome; the header counts the values, not the rows. Add another value to this check grows the group, and Split out makes one value a separate check.
- Customize changes just this check; Reset to default restores the current project default. A customized Message has its own Revert to default action.
- Remove stages removal of a check. Discard drops the guard draft. Press Compile to save a change you want to keep.
- Value renamed or removed means the check needs Replace value, or removal, before you Compile.
Handle a missing value at one node
Section titled “Handle a missing value at one node”An audio player can be optional even when the score counter is required. A node guard can skip PlayCoin when _audio is missing while letting the rest of the pickup method continue. This differs from a method guard that skips all remaining work.
Handle a missing value at one node
Select the call
Select the call or member-read node you want to protect. For example, select PlayCoin with its target wired from _audio.
Choose Null-safe read
Press Add a check. Under How should it be guarded?, choose Null-safe read to skip access when the object is missing.
Compile
Press Compile. Click the shield marker on the call’s target pin, then expand the guard row to inspect it or Remove it.
Project-wide rules
Section titled “Project-wide rules”Use guards without changing the project setup first. When you want a shared warning style, different defaults, or recognition of your team’s own C# checks, open Edit > Project Settings > Round Trip > Guard recognition. These rules are shared with the project; the toolbar gear opens personal Preferences, a different page.
Guard rules has the setup steps and a verification checklist. For one guard’s exact pattern and generated C#, expand Advanced · show code on its row. You can skip that section when working visually.
What it writes
Section titled “What it writes”If you also work in C#, this is an existing silent guard on _hud ahead of the HUD call in a RefreshHud helper on PlayerController in the Platformer sample. It has no warning call; adding or changing a check uses the pattern chosen in your project.
Round Trip also reads matching checks already in a script as guards, including a yes-or-no variable checked before work continues. A check that does not match stays visible as ordinary nodes and wires. The Guard clause pattern teaches explicit early-exit branches when you want to build the flow yourself.
When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| The picker says Every value is already required | Read the rows on the GUARDS card. | Edit an existing check, or use Add another value to this check, instead of adding a duplicate. |
| A row says Needs a return | Look for the What this method returns when it skips row. | Press Choose what to return and pick the value. |
| A row says Value renamed or removed | Look in the Variables list for the value that replaced it. | Press Replace value and pick it, or press Remove. |
| Compile is off and says Fix the pending change that needs attention before compiling | Look on the GUARDS card for a row with a warning word, such as Needs a return. | Use the button on that row, or remove the check, then Compile. |









