Variables and Score in Scratch
A step-by-step lesson on score in Scratch: make a variable, find the set and change blocks, add a point during play, reset score and lives when the game starts, show or hide the counter, and keep a high score between games.
Key takeaways
- A variable is a labelled box that stores a value your program can change
- 'set [score] to 0' puts a value in the box; 'change [score] by 1' adds to it
- Resetting the score to 0 at the green flag stops old points carrying over
- Ticking a variable's checkbox shows its value on the stage as a live counter
What is a variable?
When you play a game, something has to remember your score. Every time you catch an apple or dodge an enemy, the number on screen goes up. The thing that remembers that number is called a variable.
A variable is like a labelled box. The label is the variable's name — for example, score. Inside the box is a value, like the number 7. Your code can look inside the box, put a new value in, or change the value. Variables are one of the most important ideas in all of coding. If you would like the bigger picture across all languages, see variables explained. In this lesson we will use one to keep score in Scratch.
If Scratch is new to you, work through getting started with Scratch first, then come back here.
Step 1: Make a variable
- In Scratch, click the Variables category (it is orange) in the blocks palette.
- Click Make a Variable.
- Type the name
scoreand click OK.
A few new blocks appear, and a small counter shows up in the top-left of the stage. That counter is your variable's value, shown live. Right now it says score 0.
Step 2: Set the score to a value
The block set [score] to 0 puts a value into the box, replacing whatever was there. We want the score to start at zero, so drag this script onto your sprite:
when green flag clicked
set [score] to 0
Click the green flag. The counter resets to score 0. This matters more than it might seem: variables remember their value between runs, so if you do not reset it, last game's points carry into the new game. Setting it to 0 at the green flag gives every game a clean start.
Step 3: Change the score during play
Now the fun part — making the score go up. The block change [score] by 1 adds 1 to whatever is in the box. It is different from set:
set [score] to 5makes the score exactly 5, throwing away the old value.change [score] by 5adds 5 to the current value. If the score was 10, it becomes 15.
Let's add a point whenever you click the sprite:
when this sprite clicked
change [score] by 1
Click the green flag to reset, then click your sprite a few times. Watch the counter climb: 1, 2, 3... You are reading and updating a variable, just like a real game.
Step 4: Show or hide the score on the stage
The live counter on the stage comes from the checkbox next to the variable.
- In the Variables category, find the
scoreblock with a small checkbox beside it. - Tick the box to show the counter on the stage. Untick it to hide it.
You can also drag the counter to any corner of the stage. Showing the score lets the player always see how they are doing.
Where the score blocks are in Scratch
Every block you need for a score lives in one place: the Variables category in the block palette on the left. Its colour is orange, and it sits below Sensing and Operators.
The blocks only appear once you have made a variable. Click Make a Variable, name it score, and these five show up:
| Block | What it does |
|---|---|
set [score] to 0 | Forces the score to an exact number. Use it to reset. |
change [score] by 1 | Adds 1 to the current score. Use −1 to take a point away. |
show variable [score] | Puts the counter on the stage. |
hide variable [score] | Takes the counter off the stage without deleting it. |
(score) | The round reporter block. Drop it inside other blocks to read the value. |
The one people hunt for is change [score] by 1. It is the second orange block in the Variables palette, directly under set. If you cannot see it, you have not made a variable yet.
Setting up score and lives when the game starts
Nearly every game needs the same opening script: reset the counters, show them, then begin. Put this on the Stage or on your main sprite so it runs once:
when green flag clicked
set [score] to 0
set [lives] to 3
show variable [score]
show variable [lives]
broadcast [start game]
Two things make this work properly. First, always set, never assume — without set [score] to 0, the score keeps whatever it held when you last stopped, and your second game starts at 47. Second, the broadcast at the end lets every other sprite begin at the same moment, after the counters are ready.
Losing a life works exactly like scoring, in reverse:
when I receive [hit]
change [lives] by -1
if <(lives) = 0> then
stop [all]
You can swap stop all for broadcast [game over] if you want a Game Over screen — see broadcasting messages in Scratch.
How to delete or hide a score
There is an important difference between hiding a score and deleting it.
To take the counter off the stage but keep using the number in your code, use hide variable [score], or untick the checkbox next to the variable in the palette. The variable still exists and still counts — you just cannot see it. This is what you want for a hidden high score or a timer you display your own way.
To remove the variable completely, right-click its name in the Variables palette and choose Delete the “score” variable. Do this only when you are sure: any block still using that variable becomes broken, so delete the blocks from your scripts first, then the variable.
One more choice worth knowing. When you make a variable, Scratch asks whether it is For all sprites or For this sprite only. A score should almost always be for all sprites, so every sprite can add to the same total. Pick “this sprite only” and each clone gets its own private copy — useful for a clone's speed, wrong for a shared score.
A worked example: a clicking score game
Let's put the pieces together into a tiny complete game. The goal: click an apple as many times as you can, and win when you reach 10.
- Add the Apple sprite.
- Add this script to the apple:
when green flag clicked
set [score] to 0
forever
if (score) = 10 then
say [You win!] for 2 seconds
stop [all]
- Add this second script to the same apple:
when this sprite clicked
change [score] by 1
go to x: (pick random -200 to 200) y: (pick random -150 to 150)
Here is how it works, step by step:
- Clicking the green flag runs the first script.
set [score] to 0gives a clean start, and the forever loop begins watching the score. - Each time you click the apple, the second script runs:
change [score] by 1adds a point, then the apple jumps to a random spot so you have to chase it. - The forever loop keeps checking
if (score) = 10. As soon as the score hits 10, the apple says "You win!" andstop [all]ends the game.
Two scripts, one variable — and you have a real game with a score and a win condition.
Step 5: Add a high score (challenge level)
Want to remember the best score across many games? Make a second variable called high score. At the end of a game, check whether the score beat it:
if (score) > (high score) then
set [high score] to (score)
Because high score is not reset at the green flag, it keeps its value between games and remembers your personal best. The score resets each game, but high score does not — that is the trick.
Try it: make it your own
You now know how to keep score. Try one of these challenges:
- Lose points: add a "bomb" sprite. When clicked,
change [score] by -2. - Add lives: make a second variable called
lives, set it to 3, andchange [lives] by -1for mistakes. - Add a timer: use the built-in
timerand end the game after 30 seconds instead of at 10 points. - Speed it up: make the apple jump faster or smaller as the score climbs, so the game gets harder.
If a variable does not behave, read your blocks in order and check whether you meant set or change. Fixing problems like that is called debugging, and every coder does it. Have fun keeping score! 🔢
Quick quiz
Test yourself and earn XP
What is a variable in Scratch?
A variable is a named box that holds a value, such as a score, which your program can read and change while it runs.
What does 'set [score] to 0' do?
'set ... to' replaces the variable's current value with the new one, so 'set [score] to 0' makes the score exactly 0.
What is the difference between 'set [score] to 5' and 'change [score] by 5'?
'set' replaces the value, while 'change by' adds to the current value. If the score was 10, 'set to 5' gives 5 but 'change by 5' gives 15.
Why should you 'set [score] to 0' when the green flag is clicked?
Variables keep their value between runs, so resetting to 0 at the start makes every new game begin with a clean score.
How do you show a variable's value on the stage?
Ticking the checkbox beside a variable displays a live counter on the stage that updates as the value changes.
FAQ
A variable can hold a number, like a score or a timer, or text, like a player's name. In a game the most common variable is a number that counts points, lives or time.
Add 'set [score] to 0' right under your 'when green flag clicked' block. Variables remember their value between runs, so without a reset, last game's points carry over into the new one.
Keep exploring
More in Coding