← Game Coding
Game Coding U1-L4 Your First Game Window

Variables and Your First Click

Click a button three times and the game has to remember the three. Here's where it keeps that number, and how a click gets through to your code.

Teacher Notes

Not started yet.

Lesson Goal

Students will store the count in a whole-number variable, connect the plus button's Pressed signal to a method, and make that method add one and show the new number in the label.

Key Concepts

A variable is a labeled box. The label is its name, and what's inside can change. The type int means the box holds whole numbers.

A label's Text holds words, so the number has to be turned into words with ToString before it can be shown.

A signal is like a doorbell. The button rings Pressed when it's clicked, and += says to run a given method when that happens. The count++ shortcut also exists, but the lesson keeps the long form on purpose.

Where Students Get Stuck

Forgetting ToString, which produces an error that says an int can't be turned into a string. Adding it fixes it.

Putting the count box inside the click method, which makes it start over at zero on every click, so the label only ever shows 1. The box has to live outside, at the top of the class.

Typing the method name differently in two places. C# cares about capital letters, so OnPlusPressed and Onpluspressed are different names.

Checking the Work

When this lesson is done, Main.cs should have a count variable and a label variable at the top, the label and button connected inside the starting-up method, and a click method that adds one and updates the label.

Pressing F5 and clicking the +1 button should make the number climb by one each time, 1, 2, 3 and so on.

A parent can open Main.cs and compare it to the code in the last Watch It Worked. The Hello print line may have been removed.

Suggested Videos

Optional. Watch one first, then press Add to put it in the Visual Panel for a student.

Key Vocabulary

Variable
A named box where a program keeps something it needs to remember, and what's inside can change.
Int
The kind of variable that holds a whole number, with no decimals.
Signal
A message a node sends out when something happens, such as a button being clicked.
String
Words and letters, which is what a label's text holds, as opposed to a number.
Convert
To turn one kind of thing into another, such as a number into words so it fits in a label.
Sentence 1 of 26 Tap any sentence to start reading there
Settings

Settings

Stored on this device only. Your key stays in this browser. It is never sent to NexStudents, never shared, and never leaves your machine except to the voice provider it belongs to.

Where to get a key. Google Cloud TTS powers the built-in voices on this page and has a free monthly allowance. ElevenLabs is worth a look if you want a more natural voice and do not mind setting up an account.

Colors

Read-aloud is not available in this browser. Try Chrome or Safari.

Before You Start

The Words

The words this lesson uses. Tap a card to see what it means, then come back to them whenever a question uses one.

Part One

The Questions

0 of 0

Questions about the story you just read. Pick your answer straight off, or press Find it in the story if you would rather hunt for the sentence first.

Part Two

Vocabulary Check

0 of 0

The word cards are at the top of this page. Look back at them as often as you need, then answer.

When you finish: write the answers out in your own words — not copied word for word from the choices. Saying it your own way is the part that proves you understood it. Then use Print Answer Sheet for a one-page record of what was answered and scored.
Homework Sheet: UnavailableThere is no homework sheet for this lesson yet.