Skip to content

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:

  1. Make a Variable
    Go to the Variables section and click Make a Variable.
    Give it a name like score or playerName.

  2. Set a Starting Value
    Use the block:
    variables-1

๐Ÿ’ก Tip: Put this in the green flag block so it starts fresh!

  1. Change the Value
    Use:
    variables-1a

This is great for counting points or keeping score.

  1. Show or Hide It
    Use show variable or hide variable to control whether it appears on the screen.

๐ŸŽฎ Example:

Keeping Score in a Game:

variables-2


๐Ÿ”— 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.

Watch the video