Scratch maths: variables as number boxes
Use Scratch projects to store scores, timers and lives as changing numbers.
Quick answer
In Scratch maths, variables are like number boxes that can hold changing numbers. You can use them to store your score, timer, or lives in a game. When something happens in your project, the number in the box changes automatically.
Learning outcome
By the end of this page, you will be able to use variables in Scratch to store and change numbers like scores, lives, or timers in your projects.
- You can create a variable and give it a name
- You can see the variable appear on the screen as a number box
- You can change the number in the box when something happens
- You can use the variable in your game or project
Why this matters
Variables are like magic number boxes in Scratch that help make your projects more interesting and interactive. When you're playing a game, you want to see your score go up when you collect coins or your lives go down when you get hit. Variables make this happen!
Learning about variables helps you understand how computers store and change information. This is useful not just in coding, but in maths too. When you count objects, keep track of time, or measure things, you're already using the same kind of thinking as variables.
Variables also help you build more complex games and projects. As you grow up, you'll use variables in more advanced coding and even in subjects like science and engineering where you need to track changing values.
How it works
Variables in Scratch are like boxes that can hold numbers. Here's how to use them:
- Make a new variable: Click on "Make a Variable" in the Variables menu
- Name your variable: Give it a name like "Score" or "Lives"
- See your variable: The variable will appear on the screen as a number box
- Change the number: Use blocks to make the number go up or down
- Use the variable: Put the variable in your project to show and change it
Think of variables like a scoreboard. When you score a point, you add one to your score. When you lose a life, you take one away. Variables help you do this automatically in your Scratch project.
Examples you can see
Let's look at some examples of variables in action:
Example 1: Score in a game
Imagine you're making a game where you collect stars. You start with 0 stars. Every time you collect a star, your score goes up by 1.
| What happens | What number changes | How it changes |
|---|---|---|
| Start game | Score | 0 |
| Collect star | Score | +1 |
| Collect star | Score | +1 |
| Collect star | Score | +1 |
Example 2: Lives in a game
Now imagine a game where you have 3 lives. If you get hit, you lose one life.
| What happens | What number changes | How it changes |
|---|---|---|
| Start game | Lives | 3 |
| Get hit | Lives | -1 |
| Get hit | Lives | -1 |
| Get hit | Lives | -1 |
Example 3: Timer
Variables can also be used for a countdown timer. You start with 60 seconds and count down.
| What happens | What number changes | How it changes |
|---|---|---|
| Start timer | Timer | 60 |
| 1 second passes | Timer | -1 |
| 1 second passes | Timer | -1 |
| 1 second passes | Timer | -1 |
Try it as a mini adventure
Adventure: The Treasure Hunt Challenge
You are on a treasure hunt in a magical forest. You start with 0 coins and 3 lives. Your goal is to collect 10 coins before your lives run out.
- Start with a variable called "Coins" set to 0
- Start with a variable called "Lives" set to 3
- When you find a coin, add 1 to your Coins variable
- When you get caught by a monster, subtract 1 from your Lives variable
- When you collect 10 coins, you win!
- When your lives reach 0, you lose!
Mini challenge: Draw a simple map of your treasure hunt and write down what happens to your variables at each step. What happens when you find your first coin? What happens when you lose your first life?
Common mistakes
Here are some mistakes children often make with variables:
Mistake 1: Forgetting to create the variable
Incorrect: Trying to use a variable that doesn't exist
Why it happens: Not clicking "Make a Variable" first
Correct: Always make your variable before using it. Click "Make a Variable" and name it clearly.
Mistake 2: Using the wrong block
Incorrect: Using "change variable by 1" instead of "set variable to 1"
Why it happens: Confusing "change" with "set"
Correct: Use "set variable to" when you want to start with a specific number. Use "change variable by" when you want to add or subtract from the current number.
Mistake 3: Not seeing the variable on screen
Incorrect: Thinking the variable isn't working because it's not visible
Why it happens: Forgetting to turn on the variable display
Correct: Make sure to click the checkbox next to your variable name to show it on screen.
Mistake 4: Confusing variable names
Incorrect: Using "Score" and "Points" for the same thing
Why it happens: Not thinking carefully about naming
Correct: Choose one clear name for each variable. Keep it simple and meaningful.
Parent and carer guide
Helping your child with variables in Scratch is like helping them learn to use a new toy. Here's how to support them:
- Start simple: Begin with just one variable, like Score or Lives
- Play together: Create a simple game together, like collecting coins or avoiding obstacles
- Ask questions: "What happens when you collect a coin?" or "What happens when you lose a life?"
- Be patient: Variables can be tricky at first. It's normal to make mistakes
- Use real life: Think about games you play together - how do scores change?
- Don't rush: Let your child explore and make their own discoveries
- Encourage creativity: Let them name their variables and think of their own games
- Focus on process: Celebrate when they figure out how to make their variable work
- Remember: The goal is understanding, not perfection
- Keep it fun: Variables make projects more interesting and interactive
When your child gets frustrated, remind them that making mistakes is part of learning. Variables are like magic boxes that make their projects more exciting!
Practice questions
Try these questions to test your understanding:
- What is a variable in Scratch?
- How do you create a new variable?
- What is the difference between "set variable to" and "change variable by"?
- Give one example of when you might use a variable in a game.
- Why is it helpful to show variables on screen?
Answers:
- A variable is like a number box that can hold changing numbers
- Click "Make a Variable" in the Variables menu
- "Set variable to" gives it a specific number. "Change variable by" adds or subtracts from the current number
- Examples: Score, Lives, Timer, Health, or Coins
- It lets you see how the numbers change as your project runs
FAQ
What is a variable in Scratch?
A variable in Scratch is a name for a number box that can hold different numbers as your project runs. It's like a container that can change its contents.
Can I have more than one variable?
Yes! You can have as many variables as you need for your project. Common ones are Score, Lives, Timer, and Coins.
How do I see my variables in a project?
After creating a variable, click the checkbox next to its name to make it appear on screen. You'll see it as a number box.
What blocks do I use with variables?
You use "set variable to", "change variable by", and "show variable" blocks from the Variables menu.
Do variables work the same way in other programming languages?
Yes! The basic idea of variables is the same in most programming languages. They're a fundamental part of how computers store information.