Variables
๐จ๐ฑ Scratch: Variables
What are these?:
A variable is like a labelled container that can hold a number or word. You can make your own to keep track of scores, names, answers, and more!
๐ ๏ธ What You Can Do With It:
- ๐ฏ Keep score in a game
- โฑ๏ธ Count time or tries
- ๐ฆ Store player names or answers
- ๐ Keep track of changing values like distance or energy
๐งฐ How To Use It:
-
Make a Variable
Go to the Variables section and click Make a Variable.
Give it a name likescore
orplayerName
. -
Set a Starting Value
Use the block:
๐ก Tip: Put this in the green flag block so it starts fresh!
- Change the Value
Use:
This is great for counting points or keeping score.
- Show or Hide It
Useshow variable
orhide variable
to control whether it appears on the screen.
๐ฎ Example:
Keeping Score in a Game:
๐ Try Using It With:
- ๐ข Operators (to do maths with your variable)
- ๐ฃ Broadcast and Receive (change variables when something happens)
๐ง Make It Stick:
- Try making a simple game in which a variable controls the score
- Can you use a variable to make a timer?
๐งโโ๏ธ๐ฅ For All Sprites vs. For This Sprite Only
When you create a variable, Scratch will ask:
"For all sprites" or "For this sprite only"?
Hereโs what that means:
Option | What It Means | When To Use It |
---|---|---|
๐ฆ For all sprites | The variable can be seen and changed by any sprite in your project. | โ Great for scores, timers, and anything thatโs shared between characters or the whole game. |
๐จ For this sprite only | The variable belongs to just one sprite. Other sprites canโt see or use it. | โ Great for things like each sprite having their own health, name, or points. |
๐ฎ Example:
-
If you're making a multiplayer game, you might give each sprite their own variable:
-
player1Points
(for Player 1 only) -
player2Points
(for Player 2 only) -
If you're making a single player game, use a shared variable:
score
(for all sprites)
๐ง Make It Stick:
- Try making a simple game in which two sprites move around the screen and two different speed variables control the speed of each sprite separately so they can move at different speeds to each other.