Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning C# by Developing Games with Unity 5.x - Second Edition

You're reading from  Learning C# by Developing Games with Unity 5.x - Second Edition

Product type Book
Published in Mar 2016
Publisher Packt
ISBN-13 9781785287596
Pages 230 pages
Edition 2nd Edition
Languages

Table of Contents (20) Chapters

Learning C# by Developing Games with Unity 5.x Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Discovering Your Hidden Scripting Skills and Getting Your Environment Ready 2. Introducing the Building Blocks for Unity Scripts 3. Getting into the Details of Variables 4. Getting into the Details of Methods 5. Lists, Arrays, and Dictionaries 6. Loops 7. Object, a Container with Variables and Methods 8. Let's Make a Game! – From Idea to Development 9. Starting Your First Game 10. Writing GameManager 11. The Game Level 12. The User Interface 13. Collectables — What Next? Index

Chapter 2. Introducing the Building Blocks for Unity Scripts

A programming language like C# can appear to be very complicated at first, but in reality, there are two basic parts that form its foundation. These parts are variables and methods. Therefore, understanding these critical parts is very necessary for learning any of the other features of C#. As critical as they are, they are very simple concepts to understand. Using these variable and method foundation pieces, we'll introduce the C# building blocks that are used to create Unity scripts.

For those who get sweaty palms by just thinking of the word script, wipe your hands and relax! In this chapter, I'm going to use terms that are already familiar to you to introduce the building blocks of programming. The following are the concepts introduced in this chapter:

  • Using variables and methods in scripts

  • The class, which is a container for variables and methods

  • Turning a script into a component

  • Components that communicate using the dot syntax...

Understanding what a variable is and what it does


What is a variable? Technically, it's a tiny section of your computer's memory that will hold any information that you put there. While a game is running, it keeps track of where the information is stored, the value kept there, and the type of that value. However, for this chapter, all you need to know is how a variable works. It's very simple.

What's usually in a mailbox, besides air? Well, usually there's nothing, but occasionally there is something in it. Sometimes, there are letters, bills, a spider, and so on. The point is that what is in a mailbox can vary. Therefore, let's call each mailbox a variable.

In the game development world, some simple examples of variables might be:

  • playerName

  • playerScore

  • highestScore

Naming a variable

Using the example of the mailbox, if I asked you to see what is in the mailbox, the first thing you'd ask is, "Which one?" If I say in the Smith mailbox, the brown mailbox, or the round mailbox, you'll know...

What is a method?


When we write a script, we are making lines of code that the computer is going to execute, one line at a time. As we write our code, there will be things that we want our game to execute more than once. For example, we can write a piece of code that adds two numbers. Suppose our game needs to add those two numbers a hundred different times during gameplay. So you'd say, "Wow! I have to write the same code a hundred times to add two numbers together? There has to be a better way."

Let a method take away your typing pain. You just have to write the code to add two numbers once and then give this chunk of code a name, such as AddTwoNumbers(). Now, every time your game needs to add two numbers, don't write the code over and over; just call the AddTwoNumbers() method.

Using the term "method" instead of "function"

You are constantly going to see the words "function" and "method" used everywhere as you learn how to code.

Note

The words "function" and "method" truly mean the same thing...

Introducing the class


The class plays a major role in Unity. Most of your code will be written inside classes. Think about it like a container for variables and methods.

You just learned about variables and methods. These two items are the building blocks used in Unity scripts. The term "script" is used everywhere in discussions and documents. Look for it in the dictionary, and you will see that it can generally be described as written text. Sure enough, that's what we have. However, since we aren't just writing a screenplay or passing a note to someone, we need to learn the actual terms used in programming.

Unity calls the code it creates a C# script. However, people like me have to teach you some basic programming skills and tell you that a script is really a class.

Note

In the previous section about methods, we created a class (script) called LearningScript. It contained a couple of variables and a method. The main concept, or idea, of a class is that it's a container of data, stored in variables...

The Start(), Update(), and Awake() methods and the execution order


The Start(), Update(), and Awake() methods are called automatically. The Start() method is called on the frame when the script is enabled. For most of our components, this will be when you press the Start button in Unity.

The Awake() method is called just before the Start() method. That gives a very convenient place to set up code if you have any. The Update() method is very specific. It's called on every frame if the component is enabled. It's very useful for observing user keyboard actions, for example. As you can see in our script, in Line 16, we are checking on every frame to know whether the user has pressed the Enter key.

Let's create a new C# Script and call it LearningMethods. As you can see, the Start() and Update() methods are added automatically when you create a new script. To test them all, all that we need to do is add the Awake() method and a few other useful lines to print something on the Console panel.

As you...

Components that communicate using dot syntax


Our script has variables for holding data, and our script has methods to allow tasks to be performed. I now want to introduce the concept of communicating with other GameObjects and the components they contain. Communication between one components GameObject and another component GameObject using dot syntax is a vital part of scripting. It's what makes interaction possible. We need to communicate with other components or GameObjects to be able to use the variables and methods in other components.

What's with the dots?

When you look at code written by others, you'll see words with periods separating them. What the heck is that? It looks complicated, doesn't it. The following is an example from the Unity documentation:

transform.position.x

Note

Don't concern yourself with what the preceding code means, as that comes later. I just want you to see the dots.

This is called dot syntax. The following is another example. It's the fictitious address of my house...

Making decisions in code


The fundamental mechanism of programming is making decisions. In everyday life, we make hundreds—and possibly thousands—of decisions a day. They might be the results of simple questions such as, "Do I need an umbrella today?" or "Should I drive at the maximum motorway speed at the moment?" Let's first take a question and draw a single graph, as follows:

This is a fairly easy question. If it will be raining, I need an umbrella; otherwise, I don't. In programming, we call it an if statement. It's a way we describe to the computer what code should be executed under what conditions. The question "Will it be raining?" is the condition. When planning your code, you should always break down decision-making in to simple questions that can be answered only by a "yes" or a "no."

Note

In C# syntax, we use true or false instead of yes/no.

We now know how the simplest if statements work. Let's see how this question will look in code. Let's create a new script, name it LearningStatements...

Paper and pencil are powerful tools



We went through a few simple examples. For us humans, it's fairly simple to comprehend a few variables, if statements, and methods. Imagine, however, that you need to write a game containing many thousands of lines of code. It is very easy to get lost in your own project, trust me! There are many good practices and tools that can help you keep your project manageable. The most powerful one is planning. Plan as much as you can, write down ideas, and make notes. Draw flowcharts to break down complex decisions and you will be fine!

Summary


This chapter introduced the basic concepts of variables, methods, and the dot syntax. These building blocks are used to create scripts and classes. Understanding how these building blocks work is critical, so you don't feel you're not getting it.

We discovered that a variable name is a substitute for the value it stores, a method name is a substitute for a block of code, and when a script or class is attached to a GameObject, it becomes a component. The dot syntax is just like an address for locating GameObjects and components.

With these concepts under your belt, you can proceed to learn the details of the sentence structure, grammar, and syntax used to work with variables, methods, and the dot syntax. You also learned how to make decisions in code based on variable values. In the next chapter, we will cover the details of using variables.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning C# by Developing Games with Unity 5.x - Second Edition
Published in: Mar 2016 Publisher: Packt ISBN-13: 9781785287596
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}