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:
- a character named player that can move (see Move a character);
- a few coins: Circle objects (or images), for example coin1, coin2, coin3;
- a Text object named scoreText, at the top of the screen, with the text “Score: 0”.
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.
- Select a coin.
- In the inspector, field “Tag (Label)”, type piece.
- 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.
- In the left panel, open the Variables section and create a variable.
- 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
- Open the scene's Blueprint.
- Add the “If object touches Tag” event: target player, Tag piece.
- Add “Destroy Object”. As its target, choose The object involved in the event: that is the coin the player just touched.
- Add “Add to Variable”: variable score, Value to add = 1.
- Add “Modify Text”: target scoreText, New text =
="Score: " + score - Link the four nodes in order, output to input.
- 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
- After “Modify Text”, add the “Condition” node with Condition =
=score >= 3(3 = the number of coins). - 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
- The coin doesn't disappear: check that the tag is spelled the same everywhere (piece) and that the target of “Destroy Object” is the involved object.
- The text shows the formula: the = at the start is missing.
- The score stays at 0: check that “Add to Variable” targets the score variable.
- To compare, open the “Platform” example game that comes with Yop2D: it collects coins exactly like this.