Skip to content

2. Variables

This is the same variable idea from Unit 1, Lesson 6 — now a real block. Click Make a Variable in the Variables category and give it a name, like score.

when green flag clicked
set [score] to (0)

when this sprite clicked
change [score] by (1)

set replaces whatever value the variable held. change adds (or subtracts, with a negative number) to whatever is already there — exactly like SET score TO score + 1 in pseudocode.

Checking the box next to a variable in the palette shows it as a counter on the stage, which is handy for scoreboards, health bars, or inventory counts.

Try it

Make a variable called score, set it to 0 when the green flag is clicked, and add 1 to it every time the sprite is clicked.

Tip

When creating a variable, Scratch asks "for all sprites" or "for this sprite only." That choice matters once you have more than one sprite that needs to share — or not share — a value.

Next: Conditions →