Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Scratch

Using Variables and Data in Scratch

Final product image
What You'll Be Creating

In the previous tutorial, you continued working on a project to make a crab chase a cat around the screen, and added some interactions.

In this part we'll continue working on that project. We'll add a time limit to the project and keep score so that the user knows how many times they've managed to get the crab to nip the mouse.

What You'll Need

To complete this tutorial, you'll need:

  • a free account with Scratch
  • a web browser with access to Scratch (I recommend using a modern browser like Chrome, Firefox, Safari or Opera for the best experience)

Getting Started

We're going to be working with the project created in the previous part of this series. We'll use the assets and scripts we've created so far and add more.

Depending on whether you worked along with that tutorial, there are two ways to get started for this tutorial:

  • Open the project you created for the last tutorial and make a duplicate of that.
  • Remix the project I created to support the last tutorial.

Alternatively you can just carry on working in the same project you already created. Here's how you do each of these (you'll only need to do one).

Duplicating Your Own Project

Open Scratch and go to the project you want to duplicate. Make sure you've clicked the See Inside button so you can see the workings of the project.

In the toolbar, click File > Save as a copy:

Duplicating Your Own Project

Scratch will open your new copy. Rename it and start working on it.

Remixing My Project

Log in to Scratch and go to the page for my project on control, sensing and operators.

Click the See Inside button to view the project:

Remixing My Project

Now click the Remix button. Scratch will save a duplicate of my project as a new project in your account. Rename it and you're good to go!

Creating a Variable to Time the Game

The first thing we'll do is add a time limit to the game, by creating a variable that will start at a given number and then reduce by 1 every second. When the number gets to zero, time's up!

Select the Stage and go to the Variables blocks. Click on the Make a Variable button to display the New Variable dialog box:

New Variable

Type in the name of your variable and click the OK button.

Your variable will now be displayed on the stage, and some blocks will appear that you can use with it:

New Variable

Now create the following script for the stage:

  • when green flag clicked
  • set Timer to 30
  • repeat until Timer = 0
  • wait 1 secs
  • change Timer by -1
  • (end of repeat loop)
  • stop all

The script will look like this:

The Final Script

Now when you play the game you'll find that the timer displays a countdown in the top left of the stage, and everything stops after 30 seconds. Later in this series we'll use a broadcast block to make the backdrop change when time's up.

Using a Variable to Keep Score

The next step is to create a second variable which will keep score of the number of times the cat has touched the crab.

Start by creating another variable in the same way as you did before, and call this one Score.

Now select the cat sprite and edit the script you've already created for it.

Immediately inside the if touching Crab block and above the if answer = meow then block, add the change Score by 1 block from the Variables blocks:

Using a Variable to Keep Score

Now when you play the game, you'll have 30 seconds to play, and you'll see your score going up each time you catch the cat! There is a problem though: if you play the game again, your score will keep going up from what you scored last time, and it won't start again at zero. That's easily fixed.

Go back to your stage and drag the set Score to 0 block into its script, above the set Timer to 30 block:

Set the Timer

Now when you play the game, your score will be reset to zero every time you start. No cheating now!

Later in the series we'll use the Score variable again, and display it more prominently for the user at the end of the game.


In this tutorial you've learned how to use variables to set a timer for your game and to keep score. This make games much more challenging and fun for users.

You can use variables in many ways. By combining them with operators, you can make things happen when a variable reaches a certain number, for example. This is something you might like to experiment with in your project: see if you can work out how to stop the game when the score reaches 30.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.