Example of using variables in Scratch

Scratch Tips: The Power of Variables

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.

debugging code

What if I … ?


What If I…?

These are my favorite three words uttered by our students.  My answer is almost always the same:  “let’s find out.”

Computer programming gives students the unique ability to not only ponder about “what ifs”, but to immediately put them into action.  What if I change the order of these code blocks?  What if I change “Enderman” in this code to “EnderDragon”? (Minecraft lingo if you’re not aware)  Students can make the changes, run the program, and gain instantaneous feedback to move forward.

When students ask these types of questions I know they’re on the right track.  It means they’re engaged in what we’re doing.  It means they’re being creative.  It’s probably a good indicator of their ability to solve problems in the future.

What if I…?  Do it.  Let’s find out.

Brandon is helping his son learn about code

That Moment Your Son Learns to Read Code

One afternoon, Ryan and I gave some of our curriculum a test run on our children. When you have family, it’s easy to make them the guinea pigs or test cases for what you’re trying to accomplish. We didn’t have more than an hour, and we took our kids through the Hour of Code.

Brandon is helping his son learn about code

Taking Baby Steps

The exercises found on the site are great because they’re very visual. My oldest son would add a command to the site and check what the result would be. Move forward. Great. Now we know that was the desired action or not good enough.

“I’ll just try this to see what happens,” he said.

He didn’t always get the desired result. There were several times he tried a few things, deleted the commands or started over. We talked about it for a moment, he tried again, and it started to click.

That Moment Your Child “Gets It”

My son worked on what’s called an “if statement.” In simplest terms, think of it as “if this happens, do that.” This one was a little more complicated. It required a little more thought because there was more than one possible outcome. He had to pair two if statements together. I asked him to not look at the visual builder and look at the code itself. I asked him, “What does this code do?”

Here was his explanation:

If I can move forward. Move forward. If I can’t, then I check to see if I can turn left. If I can, I turn left. If I can’t, I turn right.

Seems like such a simple process to us because we go through these thoughts all the time in our daily lives. My son took this thought and turned it to code. What’s more impressive is he went home that day and explained it to my wife who has no coding background.

Embracing Logical Thinking and Problem Solving

The if statement is a very logical progression of a thought. Something has to be possible for it to happen. This mode of thinking helps a child work through a problem in very simple terms. Think about conversations you had with your child. Have you ever said, “If you don’t get off that couch…” and followed it up with a consequence? This if statement helped my child to think through his own consequences when he did his Hour of Code.

My child is currently 8 years old. At 8 years old, I had no idea how to write any lines of code beyond command lines in DOS. It’s very easy to think technology or programming is a skill that doesn’t fit our children, but I see so much more potential for him because he’s already 10 years ahead of my first if statement.