Scratch Tips: The Power of Variables

Example of using variables in Scratch

We like to use Scratch to help teach the basics in coding through interactive stories, games, and animations. If you’ve attended one of our courses, you’ve probably used it before. We’re going to take a moment to talk about variables and why using them will help improve your work in Scratch and any programming languages.

Using Variables Makes Things Simple

Variables can be just about anything, but in Scratch we typically use variables for numbers or text. Variables can do things like:

  • Allow you to use the same number in many places but only create or change it once
  • Change a number as many times as you want while your program is running
  • Track something that is moving on the screen
  • Say something you want when you need it
  • And more

An Example Using Variables

Imagine your game starting at level 1. Every time you score 5 points we want the game to “level up.” We could take the long way around and do math like 25 / 5 = 5, but that’s a lot of code to think of every possible number we would want to divide by. Variables let us set up things we want to use to make this feature of our game much easier to manage.

Make a variable in ScratchStep 1: Create two variables

Open up your Scratch game and click the Data section. Click Make a Variable. Name it Score. Now repeat and make one named Level.

Step 2: Set up the game

Our game is going to be simple just to show the power of variables so we’re not going to worry about making our sprite (who should be a cat) move. We’re going to make the score go up 1 every second, and at 5 seconds we’re going to level up.

Set your variables once your game startsFirst, we need to start by setting a value for each variable. Let’s begin. You can follow along by using the images.

  1. Go to Events and drag when flag clicked to our workspace on the right
  2. Go to Data and drag set level to 0 to the workspace directly below the flag
  3. Go to Data and drag set level to 0 to the workspace directly below your last block
  4. Change one to Score
  5. Change the Level to 1

Step 3: Changing our first variable

Let's count time! Using a variable to count every secondWe want the score to go up by 1 for each second that passes. This is done really easily.

  1. Go to Control and drag the forever block below your last block
  2. In Control, drag wait 1 secs to inside the new forever block
  3. Go to Data and drag change Level by 1 to after the new wait block
  4. We don’t want the Level to go up yet; change Level to Score

Hit your Green Flag or GO button to watch your score go up every second. Congrats! You’re counting time!

Step 4: Level up!

Adding an if block to our script to change level if score reaches 5Every time the Score reaches 5 we want the Level variable to go up. Essentially we’re leveling up. We’ll reset the score in this case to make it very easy to track and watch.

  1. Go to Control and drag the If…then… block after your change Score by 1 block
  2. Go to Operators and drag the equals operator into the If…then… block
  3. Go to Data and drag the Score variable (looks like a circle) to the first empty box in your new equals operator
  4. In the remaining empty box in your equals operator, type 5 (Bonus: if you make this a variable of it’s own!)
  5. Go to Data and drag change Level by 1 inside your If…then… block

Last step: Reset the score

What are we missing here? We are only leveling up if we reach a Score of 5. We need to set the Score back to 0 to start the count over.

  1. Go to Data and drag set Level to 0 to after your change Level by 1 block
  2. Change your new set Level block to set Score to 0

Hit your Green Flag or GO button to watch your score go up every second and your level adjust every time your score reaches 5. Congrats! You now have a way to use variables to track numbers, or data, and have an interaction happen.

Double Bonus: This program will run forever if you let it. How would we cap this at 10 levels?

Example of using variables in Scratch

Bonus Knowledge About Variables

Variables make a developer’s life much easier when doing complex functions and actions. It’s best practice to use variables wherever possible. This way your program becomes more flexible if you want to make little tweaks here and there. Where else could you use variables in this example we provided? Please feel free to share your projects in the comments below.

Use our Variables Example project to get started.