3. Spikes & Death

In this part, we will add a death mechanic when the player touches the spikes at the bottom of the level. We'll keep it very simple: as soon as you touch the spikes, the level gets reloaded. 

1. Adding a death event

In your player controller graph, add a Death group with the following units:

  • Custom Event is located under Events
  • Get Active Scene and Load Scene are located under Codebase > Unity Engine > Scene Management > Scene Manager
  • Get Name is located under Codebase > Unity Engine > Scene Management > Scene

(Remember you can always search for the node name instead of browsing the fuzzy finder!)

Custom events allow us to listen to an event with any name we choose - in this case, we set it to Death. Later, when creating our spikes graph, we will trigger that event. Custom events also support any number of arguments, but in this case we don't need any (we'll use them later when creating health points and damage).

2. Triggering death on spike collision

  1. Select the Spikes object in the scene view. 
  2. Add a flow machine component
  3. Create a new macro for it named Spikes
  4. Apply the changes to the prefab
  5. Delete the default Start and Update units

In that new graph, add these units:

  • On Collision Enter 2D is located under Events
  • Compare Tag is located under Codebase > Unity Engine > Game Object
  • Trigger Custom Event is located under Events

Let's recap what's happening here:

  1. We listen for a collisions on the Spikes object
  2. When a collision happens, we check if the object owning the collider is tagged Player
  3. If it does, we send that object the Death event
  4. In the player object, we listen for the Death event
  5. When we receive it, we reload the current scene

If you test your game now and fall on the spikes, you'll see that the level restarts:

This article was helpful for 43 people. Is this article helpful for you?

Question? With the Compare Tag object I see we don't fee from the On Collision Enter 2d trigger to the Invoke on Game Object Compare Tag but yet it still returns a answer. Why is this the case? and when does this rule apply


Bolt is pretty smart, and can infer a connection here.  It knows you need the output from the block, but you haven't specified "when".  So, when the value is asked for, it will travel backwards and execute the chain to provide the value.  However, there are some subtle differences.  When you specify "when" a unit should execute, the value is cached and reused for each value connection to that unit.  When you do not specify "when", the output will be recalculated each time the output gets used.

The easiest way to see this is with the Random->Range functions.  If you skip wiring the control flow but wire the output value multiple times and log it, you will notice that each log will get a different value.  As soon as you provide the control flow, the value will be cached and it will be called only once.

This means you can help prevent spaghettic-graph if you are only using the output once by just letting Bolt figure out when to call things.  (It almost always works the way it should.  There are a few cases where side-effects mean you can't, such as having a Get Variable closer to the end of a chain that winds up getting resolved before it gets set, etc), or you can use it as a cheap macro that you can re-trigger the logic multiple times.  You'll get a feel for it, so long as you know the difference.

Excellent. Very well explained. Thank you.



When one reply in emails it adds my email signature but I cannot see any way to remove in the web portal. Have my details two times now - help in removing please including this post.

Excellent tutorial!

The "Super Units" is mind-blowing, and changed everything for me. Now all I can think of is "MEGA UNIT" -- Or "Super Inception Unit" - Super Units within Super Units.

As an object oriented programmer, the concept of this is staggering when combined with visual scripting.

Well, nothing stops you from nesting super units inside other super units. :) Bolt supports infinite nesting.

These pictures of the completed graphs really helps! I had picked a non 2d version of Collision Enter and was stumped trying to figure out why it wasn't working. Somehow I missed it in the video.