- Learning C# by Developing Games with Unity 2020
- Harrison Ferrone
- 884字
- 2025-04-04 12:50:23
Defining variables
Let's start with a simple question: what is a variable? Depending on your point of view, there are a few different ways of answering that question:
- Conceptually, a variable is the most basic unit of programming, as an atom is to the physical world (excepting string theory). Everything starts with variables, and programs can't exist without them.
- Technically, a variable is a tiny section of your computer's memory that holds an assigned value. Every variable keeps track of where its information is stored (this is called a memory address), its value, and its type (for instance, numbers, words, or lists).
- Practically, a variable is a container. You can create new ones at will, fill them with stuff, move them around, change what they're holding, and reference them as needed. They can even be empty and still be useful.
A practical real-life example of a variable is a mailbox; remember those?

They can hold letters, bills, a picture from your aunt Mabel – anything. The point is that what's in a mailbox can vary: they can have names, hold information (physical mail), and their contents can even be changed if you have the right security clearance.
Names are important
Referring to the preceding photo, if I asked you to go over and open the mailbox, the first thing you'd probably ask is: which one? If I said the Smith family mailbox, or the brown mailbox, or the round mailbox, then you'd have the necessary context to open the mailbox I was referencing. Similarly, when you are creating variables, you have to give them unique names that you can reference later. We'll get into the specifics of proper formatting and descriptive naming in Chapter 3, Diving into Variables, Types, and Methods.
Variables act as placeholders
When you create and name a variable, you're creating a placeholder for the value that you want to store. Let's take the following simple math equation as an example:
2 + 9 = 11
Okay, no mystery here, but what if we wanted the number 9 to be its variable? Consider the following code block:
myVariable = 9
Now we can use the variable name, myVariable, as a substitute for 9 anywhere we need it:
2 + myVariable = 11
If you're wondering whether variables have other rules or regulations, they do. We'll get to those in the next chapter, so sit tight.
Even though this example isn't real C# code, it illustrates the power of variables and their use as placeholder references. In the next section you'll start creating variables of your own, so keep on rolling!
Time for action – creating a variable
Alright, enough theory; let's create a real variable in our LearningCurve script:
- Double-click on LearningCurve to open it in Visual Studio and add lines 7, 12, and 14 (don't worry about the syntax right now – just make sure your script is the same as the script that is shown in the following screenshot):

- Save the file using command + S on a Mac keyboard, or Ctrl + S on a Windows keyboard.
For scripts to run in Unity, they have to be attached to GameObjects in the scene. HeroBorn has a camera and directional light by default, which provides the lighting for the scene, so let's attach LearningCurve to the camera to keep things simple:
- Drag and drop LearningCurve.cs onto the Main Camera.
- Select the Main Camera so that it appears in the Inspector panel, and verify that the LearningCurve.cs (Script) component is attached properly.
- Click Play and watch for the output in the Console panel:

The Debug.Log() statements printed out the result of the simple math equations we put in between the parentheses. As you can see in the following Console screenshot, the equation that used our variable worked the same as if it was a real number:

We'll get into how Unity converts C# scripts into components at the end of this chapter, but first, let's work on changing the value of one of our variables.
Time for action – changing a variable's value
Since currentAge was declared as a variable on line 7, the value it stores can be changed. The updated value will then trickle down to wherever the variable is used in code; let's see this in action:
- Stop the game by clicking the Play button if the scene is still running.
- Change Current Age to 18 in the Inspector panel and play the scene again, looking at the new output in the Console panel:

The first output will still be 31, but the second output is now 19 because we changed the value of our variable.
The goal here wasn't to go over variable syntax but to show how variables act as containers that can be created once and referenced elsewhere. We'll go into more detail in Chapter 3, Diving into Variables, Types, and Methods.
Now that we know how to create variables in C# and assign them values, we're ready to pe into the next important programming building block: methods!