← Yop2D

How to collect coins and count the score?

Beginner guide · no code

Français · English · Español · Deutsch · Italiano · Português · Русский · 中文 · 日本語

When the player touches a coin, we want three things: the coin disappears, the score goes up by 1, and the new score shows on the screen.

You need:

Step 1: give all coins the same tag

A tag is a label. Thanks to it, one single piece of Blueprint works for every coin, even if you add 50 of them.

  1. Select a coin.
  2. In the inspector, field “Tag (Label)”, type piece.
  3. Do the same for each coin (the “List” button next to the field shows the tags already used).

Step 2: create the “score” variable

A variable is a box that keeps a number in memory during the game.

  1. In the left panel, open the Variables section and create a variable.
  2. Name: score · Scope: Local · Type: Integer · Starting value: 0.
👉 Local = the variable only exists in this scene. Choose Global if the score must carry on to the next level (see Go to the next level).

Step 3: the Blueprint

  1. Open the scene's Blueprint.
  2. Add the “If object touches Tag” event: target player, Tag piece.
  3. Add “Destroy Object”. As its target, choose The object involved in the event: that is the coin the player just touched.
  4. Add “Add to Variable”: variable score, Value to add = 1.
  5. Add “Modify Text”: target scoreText, New text = ="Score: " + score
  6. Link the four nodes in order, output to input.
  7. Save, then start the game and go touch a coin.
If object touches Tagtarget: player · Tag: piece
→
Destroy ObjectImplied Object
→
Add to Variablescore · + 1
→
Modify TextscoreText
👉 The = sign at the start of the text tells Yop2D to calculate the text instead of writing it as it is. Without it, the screen would show "Score: " + score word for word. The quotes surround the part that does not change.
👉 The event only runs once per contact: staying on a coin does not count several points.

Bonus: win when you have all the coins

  1. After “Modify Text”, add the “Condition” node with Condition = =score >= 3 (3 = the number of coins).
  2. Link its True output to a “Small message at the bottom of the screen” node with the message You win!, or to “Next scene”.
Modify TextscoreText
→
Condition=score >= 3
True →
Small message at the bottom of the screenYou win!

If it doesn't work