Skip to main content
‹ Back to Tutorials
Intermediate Unity

Unity 3D + Playmaker - Runner Series - Stacking Donuts? Why not? Let's add stacking mechanics. P2

Aug 5, 2021
About this tutorial

In this tutorial, I'll use Unity and Playmaker and continue to add to the Stacking mechanic in the game by making donuts visible based on the variable int number. . Also check out How to Make a Bounce Game Series https://bit.ly/39HIYBU. In this tutorial series, I am covering a base runner game, b

Written Guide

Part two of the donut-stacker mechanic makes the stack visible. The previous episode set up a donuts int variable that goes up when the runner hits a donut and down when it hits an apple; this one maps that number onto a stack of donut models so the player can actually see their haul grow and shrink.

Step 1 — The plan: one state per stack size

The stack is a set of pre-placed donut models on the character, and each one's Mesh Renderer gets toggled — the objects are always there, just invisible until earned. On the game manager, after the add/subtract states finish, route both into a new Donut Compare state. Its job: read the donuts number and jump to a state that shows exactly that many donuts — zero donuts visible at 0, one at 1, and so on up the stack.

Step 2 — Build the zero case

In Donut Compare, add an Int Compare against the donuts variable. If it equals 0, fire a new event named d0 and transition to a Donuts Zero state. There, use Set Visibility actions — one per donut game object, since each has to be addressed individually — with visibility unchecked and reset-on-exit off. That unchecks the mesh renderer, hiding the donut while leaving the object in place. Finish with a finished transition straight back to the detection state at the start.

Step 3 — Duplicate for each count

The remaining cases are copy-paste with edits. Duplicate the Donuts Zero state, add another Int Compare in Donut Compare for equals 1 firing a d1 event, and in the d1 state turn donut one's visibility on while donuts two through four stay off. The important detail: every state sets the visibility of every donut explicitly, on or off, so it doesn't matter which state you arrived from — the stack always ends up correct. Repeat the pattern for each count up to your maximum.

Step 4 — Watch for the classic mistakes

Two bugs cropped up while building this, and they're worth checking in your own version. First, some Set Visibility actions were accidentally set to Toggle instead of an explicit on/off — toggle flips whatever the current state is, which desyncs the stack fast; always set visibility directly. Second, it's easy to build the display states and forget to add the matching comparisons and transitions in Donut Compare, which leaves counts 3 and 4 silently doing nothing.

Step 5 — The full loop, tested

Recapping the flow: the first state listens for donut and apple trigger events; hitting a donut first checks whether the variable is already at the max (10) — if so it goes straight back to collecting, if not it adds and heads to Donut Compare; hitting an apple subtracts, with zero as the floor. The compare state then switches on the right number of donut meshes and loops back to detection. Test by collecting up to the cap, then letting apples strip the stack back down to nothing — up, down, and clamped at both ends.

Unity tools by Bliz Studio
Built for our own games — now on the Asset Store

Multi Tag — unlimited hierarchical tags for any GameObject. Hierarchy Pro — style your whole hierarchy with rules. Made by a Unity dev, for Unity devs.

Explore our Unity assets →