Any programming language starts off looking like ancient Greek to the unaccustomed eye, and C# is no exception. The good news is that underneath the initial mystery, all programming languages are made up of the same essential building blocks. Variables, methods, and classes (or objects) make up the DNA of conventional programming; understanding these simple concepts opens up an entire world of diverse and complex applications. After all, there are only four different DNA nucleobases in every person on earth; yet, here we are, unique organisms to the last.
If you're new to programming, there's going to be a lot of information coming at you in this chapter, and this could mark the first lines of code that you've ever written. The point is not to overload your brain with facts and figures; it's to give you a holistic look at...
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...
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
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...
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.
Now that we know how to create variables in C# and assign them values, we're ready to dive into the next important programming building block...
On their own, variables can't do much more than keep track of their assigned values. While this is vital, they are not very useful on their own in terms of creating meaningful applications. So, how do we go about creating actions and driving behavior in our code? The short answer is by using methods.
Before we get to what methods are and how to use them, we should clarify a small point of terminology. In the world of programming, you'll commonly see the terms method and function used interchangeably, especially in regards to Unity. Since C# is an object-oriented language (this is something that we'll cover in Chapter 5, Working with Classes and Object-Oriented Programming), we'll be using the term method for the rest of the book to conform to standard C# guidelines.
Methods drive actions
Similarly to variables, defining programming methods can be tediously long-winded or dangerously brief; here's another three-pronged approach to consider:
- Conceptually, methods are how work gets done in an application.
- Technically, a method is a block of code containing executable statements that run when the method is called by name. Methods can take in arguments (also called parameters), which can be used inside the method's scope.
- Practically, a method is a container for a set of instructions that run every time it's executed. These containers can also take in variables as inputs, which can only be referenced inside the method itself.
Taken all together, methods are the bones of any program – they connect everything and almost everything is built off of their structure.
Methods are placeholders too
Let's take an oversimplified example of adding two numbers together to drive the concept home. When writing a script, you're essentially laying down lines of code for the computer to execute in sequential order. The first time you need to add two numbers together, you could just brute-force it like in the following code block:
someNumber + anotherNumber
But then you conclude that these numbers need to be added together somewhere else. Instead of copying and pasting the same line of code, which results in sloppy or "spaghetti" code and should be avoided at all costs, you can create a named method that will take care of this action:
someNumber + anotherNumber
Now AddNumbers is holding a place in memory, just like a variable; however, instead of a value, it holds a block of instructions. Using the name of the method (or calling it) anywhere in a script puts the stored instructions at your fingertips without having to...
Time for action – creating a simple method
Let's open up LearningCurve again and see how a method works in C#. Just like with the variables example, you'll want to copy the code into your script exactly as it appears in the following screenshot. I've deleted the previous example code to make things neater, but you can, of course, keep it in your script for reference:
- Open up LearningCurve in Visual Studio and add in lines 8, 13, and 16 - 19.
- Save the file, and then go back and hit Play in Unity to see the new Console output:
You defined your first method on lines 16 to 19 and called it on line 13. Now, wherever AddNumbers() is called, the two variables will be added together and printed to the console, even if their values change:
Go ahead and try out different variable values in the Inspector panel to see this in action! More details on the actual code syntax of what you just wrote are coming...
We've seen how variables store information and how methods perform actions, but our programming toolkit is still somewhat limited. We need a way of creating a sort of super container that has its variables and methods that can be referenced from the container itself. Enter classes:
- Conceptually, a class holds related information, actions, and behaviors inside a single container. They can even communicate with each other.
- Technically, classes are data structures. They can contain variables, methods, and other programmatic information, all of which can be referenced when an object of the class is created.
- Practically, a class is a blueprint. It sets out the rules and regulations for any object (called an instance) created using the class blueprint.
You've probably realized that classes surround us not only in Unity but in the real world as well. Next, we'll take a look at the most common Unity class and how they function in the wild.
A common Unity class
Before you wonder what a class looks like in C#, you should know that you've been working with a class this whole chapter. By default, every script created in Unity is a class, which you can see from the class keyword on line 5:
public class LearningCurve: MonoBehavior
MonoBehavior just means that this class can be attached to a GameObject in the Unity scene. In C#, classes can exist on their own, which we'll see when we create standalone classes in Chapter 5, Working with Classes and Object-Oriented Programming.
Classes are blueprints
For our last example, let's think about a local post office. It's a separate, self-contained environment that has properties, such as a physical address (a variable), and the ability to execute actions, such as sending in your secret decoder ring voucher (methods).
This makes a post office a great example of a potential class that we can outline in the following block of pseudocode:
Address = "1234 Letter Opener Dr."
The main takeaway here is that when information and behaviors follow a predefined blueprint, complex actions and inter-class communication becomes possible.
For instance, if we had another class that wanted to send a letter through our PostOffice class, it wouldn't have to wonder where to go to fire this action. It could simply call the SendMail function from the PostOffice class, as follows:
Working with comments
You might have noticed that LearningCurve has two odd lines of gray text (10 and 21 in the last screenshots) starting with two backslashes, which were created by default with the script. These are code comments, a very powerful, if simple, tool for programmers.
In C#, there are a few ways that you can use to create comments, and Visual Studio (and other code editing applications) will often make it even easier with built-in shortcuts.
Some professionals wouldn't call commenting an essential building block of programming, but I'll have to respectfully disagree. Correctly commenting out your code with meaningful information is one of the most fundamental habits a new programmer should have.
The single-line comment is exactly what's already in LearningCurve:
// This is a single-line comment
Visual Studio doesn't see lines starting with two backslashes (without empty space) as code, so you can use them as much as needed.
Since it's in the name, you'd be right to assume that single-line comments only apply to one line of code. If you want multi-line comments, you'll need to use a backslash and an asterisk as opening and closing characters around the comment text:
/* this is a
multi-line comment */
Seeing example comments is good, but putting them in your code is always better. It's never too early to start commenting!