Loops
Repeat work with a simple From to count, then explore the advanced C-Style view for general C# for-loop headers.
On this page
Want a row of ten coins? Start with for in its From to view: choose where to start counting, where to stop, and how much to move the count each time. Connect the work once; the loop repeats it. You do not need to write a C# expression.
Round Trip makes this common case simple on purpose. The advanced C-Style view keeps the flexibility of a C# for loop for cases that are not a simple count, without making everyone work with that extra detail. They are two views of the same kind of loop, not two programming languages. From to is the place to start.
Jump to: Build a counted loop · Advanced: C-Style · Other loop shapes · C# reference.
Count with From to
Section titled “Count with From to”In Unity, open Tools > Round Trip > Graph view. Press Cmd/Ctrl+O, choose FlowExamples from the Platformer sample, then choose SpawnCoinRow under Methods in the left rail.
| Control | Plain-language meaning |
|---|---|
| first index | The number to start at. Use 0 for the first position in a list. |
| last index | The boundary to stop at. A wire can supply a value such as count or collision.contactCount; it need not be a typed number. |
| step | How far the counter moves on each pass. 1 visits each position. |
| index output | The current number to give to the work inside the loop. |
| Loop body output | The work to repeat. White Exec wires say what runs next. |
| Completed output | What runs once after the loop finishes. |
Select the yellow node, then find Loop in the right-hand details rail.
How to use it
Section titled “How to use it”Build the simple counted loop first. Start in a method whose canvas holds only its magenta entry node, titled with the method’s name. The steps below use FlowExamples.SpawnCoinRow as the finished comparison.
Build a counted for loop
Place a for loop
Right-click empty canvas to the right of the entry node. In All actions, pick for under flow control, or type
forin Search and pick the for row.
5 stepsFull size ↗Wire it after the entry node
Drag from the entry node’s white Exec output to the loop’s Exec input.
4 stepsFull size ↗Set the range
Set the range. Type into first index, last index or step, or wire a number into a range pin.
Place the repeated work
Place the work that repeats, then wire Loop body to its Exec input. Chain any other repeated work after it.
7 stepsFull size ↗Feed the current position
Wire index to the input that needs the current number. If something should happen once afterwards, wire Completed to it.
4 stepsFull size ↗Check the loop's details
Select the loop. In the right rail’s Loop section, keep Loop type on From to and check Inclusive against the boundary you intended.
Compile
Press Compile in the top toolbar. Then check that the loop covers the range, repeats the work, and continues to the right place afterward.
Advanced: C-Style loops
Section titled “Advanced: C-Style loops”Why keep a second view?
Section titled “Why keep a second view?”A simple count is the most common use, but C# for loops can do more: set up several variables, test a compound condition, use a calculated update, or omit a header clause. Forcing all of those into first index / last index / step would either lose their meaning or make the everyday counting node harder to use.
From To keeps the common case easy; C-Style preserves the more general C# structure. The name refers to the three-part loop notation shared with C, not to a change of language: Round Trip still reads and writes C#. This design avoids restricting loops to a simplified range; it is not a promise that every other C# feature is editable. See supported C# for that boundary.
Read the advanced controls
Section titled “Read the advanced controls”| C-Style part | When it runs | What it means |
|---|---|---|
| initializer | Once, before the first check | Set up the loop’s starting state. Declared variables and their starting-value inputs are shown where the header has them. |
| Condition | Before each pass | A true/false test: repeat only while it is true. An omitted condition has no condition pin. |
| iterator | After each completed pass, including a continue | Update the loop’s state before testing again. Follow this exec output to the update nodes. |
| Loop body / Completed | Repeated work / after the loop | The same roles as in From To. |
For example, “place coins while there are positions left and the row is not full” combines a numeric boundary with an additional check. The condition requires both checks to pass. A break inside a simple From To body is also a valid way to stop early; you do not have to use C-Style just because the body makes a decision.
Switch views without rewriting the loop
Section titled “Switch views without rewriting the loop”- Open the sample’s
FlowExamples.SpawnCoinRowand select its yellow FOR node. - In the right rail, open Loop > Loop type and choose C-Style.
- Choose From to to return to the compact count.
Not every loop offers both choices. Round Trip offers From to when it recognizes one counter moving by a fixed step toward a bound, including bounds supplied by values. A more general source loop offers only C-Style: inventing a range for it would misrepresent the code.
If a switch would lose a value or wire you authored on a mode-specific pin, Round Trip refuses it with Can’t change for loop type and a reason. Keep the current view, or deliberately undo the affected edit before switching; do not disconnect useful work just to make a screenshot match.
Finish an Inclusive edit before comparing views. Switching to C-Style cancels a pending From to Inclusive change and returns to the source’s comparison. Compile the intended behavior first, or undo that draft before exploring the other view.
Example: a compound condition
Section titled “Example: a compound condition”This separate documentation variation, FlowExamples.SpawnCoinRowUntilFull, adds “and the row is not full” to the condition. It was captured in an isolated Platformer copy; it is not an extra method shipped in the sample. The actual selector offers only C-Style, and the Inputs section spells out the compound test.

C# example: the three header parts and the compound condition
The parts between the parentheses are initializer int i = 0, Condition i < count && !RowIsFull(i), and iterator i++. && means both conditions must hold; ! means “not.” The body runs only while there are positions left and the row is not full. Round Trip preserves this header rather than pretending the loop has just a numeric end boundary.
Other loop shapes
Section titled “Other loop shapes”Use for when you need a count or a position number. The flow control action menu also offers:
- foreach: visit each item in a list or array. Wire the collection into array and use Array element for the current item.
- while: repeat while a true/false Condition holds, checking before every pass. It can run zero times.
- do while: do the work once, then check Condition before repeating. It always runs at least once, even when the initial condition is false.
For while and do while, wire a Boolean result into the red Condition pin. A new unwired, unticked condition means false: while skips its body, whereas do while still runs its first pass.
Inside any loop body, break leaves the innermost loop and continues from Completed; continue skips the rest of that pass and moves to the next one. EXIT ends the whole method instead. FlowExamples.FillCoinRow and CollectRemaining show break and continue respectively.
Compare these separate FlowExamples methods; the slides are alternatives, not successive edits. The counted for and break examples sit beside their C# in What it writes.
What it writes
Section titled “What it writes”If you also work in C#, this is what lands in the script. FlowExamples.SpawnCoinRow and FlowExamples.FillCoinRow are written as:
The nodes reached by Loop body become the block inside the loop; Completed leads to the statement after it. A break node writes break; and a continue node writes continue;. Both From to and C-Style write a C# for statement. The view choice itself is layout state. It is not a second implementation of the loop.
When it goes wrong
Section titled “When it goes wrong”When it goes wrong
| Symptom | Check | Fix |
|---|---|---|
| Work that should run once runs on every pass. | Follow its white wire back. It probably starts at loop body. | Drag the wire end off loop body and onto completed. |
| A loop node shows a condition pin instead of last index. | Select the loop and read Loop > Loop type in the right rail. | Choose From to if offered and you want the compact count. C-Style is the advanced view. It is not an error; a source loop that is not a recognized range offers only C-Style. |
| A while loop never runs. | Look at its condition pin. An unticked checkbox with no wire means false; a gray dash means no value has been given yet. | Wire a red value into condition, or tick the box if you mean to stop the loop with a break inside it. |
| A break or continue affects the wrong loop. | Follow its white wire back to see which loop body it hangs from. | Rewire it onto the body of the loop you meant. |
- Branch: decide inside the body whether to act on this pass.
- Switch: send one value down one of several paths.
- Scan a list: build the contact scan step by step.



















