Variables
A variable
is a way of storing numbers and/or text.
You can make your own variables and choose whether they show on the Stage:
Add a variable in Scratch
-
Click on Variables in the Code tab, then click on Make a Variable.
-
Type in the name of your variable. You can choose whether you would like your variable to be available to all sprites, or to only this sprite. Press OK.
-
Once you have created the variable, it will be displayed on the Stage, or you can untick the variable in the Scripts tab to hide it.
You should think about whether you need to set a starting value when you make a variable:
Make a variable and set a start value
Click on Variables in the Code tab, then click on Make a Variable.
Type in the name of your variable. You can choose whether you would like your variable to be available to all sprites, or to only this sprite. Press OK.
The variable will show on the Stage:
If you want to hide the variable on the Stage, uncheck the box next to the variable in the Variables
blocks menu.
Setting a start value
If your variable should have the same starting value every time your project is run, then add a script to set it:
You can rename a variable if you change your mind:
Rename a variable
Sometimes you think of a better name for a variable.
You can give a variable a new name: go to the Variables
blocks menu, right-click (or on a tablet, tap and hold) on the variable
, and choose Rename variable.
This will change the name of the variable in all the blocks in which you have used it.
Tip: Make sure you know the difference between set
and change by
. set
will replace the value stored in a variable. change by
will change the value of a number variable by the amount you choose, change by
1
will add one to a variable. change by
-1
will take one away from a variable.
Tip: Variables on the Stage always appear in a layer above all of the sprites. If you have a moving sprite, the sprite will go under any variables on the Stage.
You can use a slider to control the value in a variable.
Use a slider to set a variable
Right-click on your variable
on the Stage.
Select slider in the menu.
Tip: On a tablet, double tap on the variable
on the Stage to change between the display options.
Right-click again on the variable
displayed on the Stage and select change slider range.
To set the range, specify a minimum and maximum value.
For example, a minimum of 1
and a maximum of 10
work fairly well for how quickly a sprite spins.
The value range that is right for your variable depends on what you are using it for. Sometimes you will need to experiment a little to get it right, but remember that you can change it at any time.
Drag the slider to a convenient position on the Stage.
Tip: If you are using a computer with a keyboard, then you can use the arrow keys to change the value of a slider by 1
. First, click on the slider to select it, then use the Left arrow key to change the value by -1
and the Right arrow key to change the value by 1
.
More ideas for using variables:
Create a high score in Scratch
It’s fun to keep track of a high score in a game.
Let’s say you have a variable called score
, which gets set to zero at the beginning of each game.
Add another variable called high score
.
At the end of the game (or whenever you want to update the high score), you’ll need to check whether you have a new high score
.
Join text and variables in Scratch
Set a variable with a button
Change a variable in a loop