Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learning C# by Developing Games with Unity 3D Beginner's Guide
Learning C# by Developing Games with Unity 3D Beginner's Guide

Learning C# by Developing Games with Unity 3D Beginner's Guide: The beauty of this book is that it assumes absolutely no knowledge of coding at all. Starting from very first principles it will end up giving you an excellent grounding in the writing of C# code and scripts.

eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning C# by Developing Games with Unity 3D Beginner's Guide

Chapter 2. Introducing the Building Blocks for Unity Scripts

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

For those people who get sweaty palms 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 in a script
  • Using methods in a script
  • The class which is a container for variables and methods
  • Turning a script into a Component
  • Components...

Using the term method instead of function

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

Note

The words function and method truly mean the same thing in Unity. They do the same thing.

Since you are studying C#, and C# is an Object-Oriented Programming (OOP) language, I will use the word "method" throughout this book, just to be consistent with C# guidelines. It makes sense to learn the correct terminology for C#. Also, UnityScript and Boo are OOP languages. The authors of the Scripting Reference probably should have used the word method instead of function in all documentation.

Note

From now on I'm going to use the words method or methods in this book. When I refer to the functions shown in the Scripting Reference, I'm going to use the word method instead, just to be consistent throughout this book.

Understanding what a variable does in a script

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

Understanding what a variable does in a script

What's usually in a mailbox, besides air? Well, usually there's nothing but occasionally there is something in it. Sometimes there's money (a paycheck), bills, a picture from aunt Mabel, a spider, and so on. The point is what's in a mailbox can vary. Therefore, let's call each mailbox a variable instead.

Naming a variable

Using the picture of the country mailboxes, if I asked you to see what is in the mailbox, the first thing you'd ask is which one? If I said in the Smith mailbox, or the brown mailbox, or the round mailbox, you'd know exactly which mailbox...

Time for action – creating a variable and seeing how it works

Let's see how this actually works in our script. Don't be concerned about the details of how to write this, just make sure your script is the same as the script shown in the next screenshot.

  1. In the Unity Project panel, double-click on LearningScript.
  2. In MonoDevelop, write the lines 6, 11, and 13 from the next screenshot.
  3. Save the file.
Time for action – creating a variable and seeing how it works

To make this script work, it has to be attached to a GameObject. Currently, in our State Machine project we only have one GameObject, the Main Camera. This will do nicely since this script doesn't affect the Main Camera in any way. The script simply runs by virtue of it being attached to a GameObject.

  1. Drag LearningScript onto the Main Camera.
  2. Select Main Camera so that it appears in the Inspector panel.
  3. Verify whether LearningScript is attached.
  4. Open the Unity Console panel to view the output of the script.
  5. Click on Play.

The preceding steps are shown in the following screenshot:

Time for action – creating a variable and seeing how it works

What...

Time for action – changing the number 9 to a different number

Since myNumber is a variable, the value it stores can vary. If we change what is stored in it, the answer to the equation will change too. Follow the ensuing steps:

  1. Stop the game and change 9 to 19.
  2. Notice that when you restart the game, the answer will be 30.

What just happened?

You learned that a variable works by simple process of substitution. There's nothing more to it than that.

We didn't get into the details of the wording used to create myNumber, or the types of variables you can create, but that wasn't the intent. This was just to show you how a variable works. It just holds data so you can use that data elsewhere in your script. We'll get into the finer details of variables in Chapter 3, Variables in Detail.

Have a go hero – changing the value of myNumber

In the Inspector panel, try changing the value of myNumber to some other value, even a negative value. Notice the change in answer in...

Using a method in a script

Methods are where the action is and where the tasks are performed. Great, that's really nice to know but what is a method?

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 we want our game to execute more than once. For example, we can write a code that adds two numbers. Suppose our game needs to add the two numbers together a hundred different times during the game. So you say, "Wow, I have to write the same code a hundred times that adds 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 our game needs to add two numbers, don't write the code over and over, just call the AddTwoNumbers() method.

Time for action – learning how a method works

We're going to edit LearningScript again. In the following screenshot, there are a few lines of code that look strange. We are not going to get into the details of what they mean in this chapter. We will discuss that in Chapter 4, Getting into the Details of Methods. Right now, I am just showing you a method's basic structure and how it works:

  1. In MonoDevelop, select LearningScript for editing.
  2. Edit the file so that it looks exactly like the following screenshot.
  3. Save the file.
Time for action – learning how a method works

What's in this script file?

In the previous screenshot, lines 6 and 7 will look familiar to you; they are variables just as you learned in the previous section. There are two of them this time. These variables store the numbers that are going to be added.

Line 16 may look very strange to you. Don't concern yourself right now with how this works. Just know that it's a line of code that lets the script know when the Return/Enter key is pressed....

Using the term method instead of function


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

Note

The words function and method truly mean the same thing in Unity. They do the same thing.

Since you are studying C#, and C# is an Object-Oriented Programming (OOP) language, I will use the word "method" throughout this book, just to be consistent with C# guidelines. It makes sense to learn the correct terminology for C#. Also, UnityScript and Boo are OOP languages. The authors of the Scripting Reference probably should have used the word method instead of function in all documentation.

Note

From now on I'm going to use the words method or methods in this book. When I refer to the functions shown in the Scripting Reference, I'm going to use the word method instead, just to be consistent throughout this book.

Understanding what a variable does in a script


What is a variable? Technically, it's a tiny section of your computer's memory that will hold any information you put there. While a game runs, it keeps track of where the information is stored, the value kept there, and the type of the value. However, for this chapter, all you need to know is how a variable works in a script. 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's money (a paycheck), bills, a picture from aunt Mabel, a spider, and so on. The point is what's in a mailbox can vary. Therefore, let's call each mailbox a variable instead.

Naming a variable

Using the picture of the country mailboxes, if I asked you to see what is in the mailbox, the first thing you'd ask is which one? If I said in the Smith mailbox, or the brown mailbox, or the round mailbox, you'd know exactly which mailbox to open to retrieve what is inside. Similarly, in...

Time for action – creating a variable and seeing how it works


Let's see how this actually works in our script. Don't be concerned about the details of how to write this, just make sure your script is the same as the script shown in the next screenshot.

  1. In the Unity Project panel, double-click on LearningScript.

  2. In MonoDevelop, write the lines 6, 11, and 13 from the next screenshot.

  3. Save the file.

To make this script work, it has to be attached to a GameObject. Currently, in our State Machine project we only have one GameObject, the Main Camera. This will do nicely since this script doesn't affect the Main Camera in any way. The script simply runs by virtue of it being attached to a GameObject.

  1. Drag LearningScript onto the Main Camera.

  2. Select Main Camera so that it appears in the Inspector panel.

  3. Verify whether LearningScript is attached.

  4. Open the Unity Console panel to view the output of the script.

  5. Click on Play.

The preceding steps are shown in the following screenshot:

What just happened?

In the...

Time for action – changing the number 9 to a different number


Since myNumber is a variable, the value it stores can vary. If we change what is stored in it, the answer to the equation will change too. Follow the ensuing steps:

  1. Stop the game and change 9 to 19.

  2. Notice that when you restart the game, the answer will be 30.

What just happened?

You learned that a variable works by simple process of substitution. There's nothing more to it than that.

We didn't get into the details of the wording used to create myNumber, or the types of variables you can create, but that wasn't the intent. This was just to show you how a variable works. It just holds data so you can use that data elsewhere in your script. We'll get into the finer details of variables in Chapter 3, Variables in Detail.

Have a go hero – changing the value of myNumber

In the Inspector panel, try changing the value of myNumber to some other value, even a negative value. Notice the change in answer in the Console.

Left arrow icon Right arrow icon

Key benefits

  • You've actually been creating scripts in your mind your whole life, you just didn't realize it. Apply this logical ability to write Unity C# scripts
  • Learn how to use the two primary building blocks for writing scripts: the variable and the method. They're not mysterious or intimidating, just a simple form of substitution
  • Learn about GameObjects and Component objects as well as the vital communication between these objects using Dot Syntax. It's easy, just like addressing a postal letter
  • Stay logically organized by utilizing a State Machine for your code. Use the simple concept of a State to control your Unity project. You will definitely save time by knowing where your code is located
  • With your new knowledge of coding, you will be able to look at Unity's Scripting Reference code examples with confidence

Description

For the absolute beginner to any concept of programming, writing a script can appear to be an impossible hurdle to overcome. The truth is, there are only three simple concepts to understand: 1) having some type of information; 2) using the information; and 3) communicating the information. Each of these concepts is very simple and extremely important. These three concepts are combined to access the feature set provided by Unity. "Learning C# by Developing Games with Unity 3D Beginner's Guide" assumes that you know nothing about programming concepts. First you will learn the absolute basics of programming using everyday examples that you already know. As you progress through the book, you will find that C# is not a foreign language after all, because you already know the words. With a few keywords and using substitution, before you know it, you'll be thinking in code. The book starts by explaining in simple terms the three concepts you need for writing C# code and scripts: 1) variables to hold information; 2) methods (functions) to use the information; and 3) Dot Syntax to communicate the information where it's needed. The book builds on these concepts to open up the world of C# coding and Unity scripting. You will use this new power to access the features provided in Unity's Scripting Reference. The first half of this book is devoted to the code writing beginner. The concepts of variables, methods, Dot Syntax, and decision processing are fully explained. Since C# is an actual programming language, we take advantage of this to develop a State Machine to help control and organize each phase of a Unity project. Once the basic programming concepts are established and we have some State Machine organization, the features and power of Unity are accessed using the Scripting Reference. If you're looking to learn C# for Unity then this is the book that offers everything you need and more - so discover what C# offers today and see your work come to life as complete games!

Who is this book for?

This book is for the total beginner to any type of programming, focusing on the writing of C# code and scripts only. There are many parts that make up the Unity game engine. It is assumed that the reader already knows their way around Unity's user interface. The code editor used in this book is the MonoDevelop editor supplied by Unity.

What you will learn

  • Understand what a variable is and how it works
  • Learn about methods and functions is and how they are used to manipulate information
  • Learn the concept of an object, a component of a GameObject, and the class they come from
  • Learn about communication between objects using Dot Syntax
  • Understand how to make decisions in code
  • Learn how to use a State Machine to control and organize a Unity project
  • Master the Scripting Reference to bring GameObjects to life
  • Learn how to use the Unity Physics engine for moving and detecting GameObject collisions and triggers
  • Display information on the game screen

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 25, 2013
Length: 292 pages
Edition : 1st
Language : English
ISBN-13 : 9781849696593
Vendor :
Epic Games
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 25, 2013
Length: 292 pages
Edition : 1st
Language : English
ISBN-13 : 9781849696593
Vendor :
Epic Games
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 109.98
Unity 3.x Game Development Essentials
$54.99
Learning C# by Developing Games with Unity 3D Beginner's Guide
$54.99
Total $ 109.98 Stars icon
Banner background image

Table of Contents

15 Chapters
1. Discovering Your Hidden Scripting Skills Chevron down icon Chevron up icon
2. Introducing the Building Blocks for Unity Scripts Chevron down icon Chevron up icon
3. Getting into the Details of Variables Chevron down icon Chevron up icon
4. Getting into the Details of Methods Chevron down icon Chevron up icon
5. Making Decisions in Code Chevron down icon Chevron up icon
6. Using Dot Syntax for Object Communication Chevron down icon Chevron up icon
7. Creating the Gameplay is Just a Part of the Game Chevron down icon Chevron up icon
8. Developing the State Machine Chevron down icon Chevron up icon
9. Start Building a Game and Get the Basic Structure Running Chevron down icon Chevron up icon
10. Moving Around, Collisions, and Keeping Score Chevron down icon Chevron up icon
11. Summarizing Your New Coding Skills Chevron down icon Chevron up icon
A. Initial State Machine files Chevron down icon Chevron up icon
B. Completed code files for Chapters 9 and 10 Chevron down icon Chevron up icon
C. Pop Quiz Answers Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(50 Ratings)
5 star 34%
4 star 32%
3 star 20%
2 star 4%
1 star 10%
Filter icon Filter
Top Reviews

Filter reviews by




Justin Mar 09, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
To start this book does not cover the unity interface per the title. I just learned Unity's interface & was ready to build some projects! However when it came time to make my new GameObjects do any thing I was stuck.It was like I hit a brick wall & though there is a lot of documentation on the web I did not fully understand the code "C#" or how it was to be applied.This book really did it for me while I was climbing over the wall I had hit.Not only will it help you draw some life out of your project , you will also learn some true C# fundamentals.I think even if you know some about C# in the Desktop , App World this will still help you to harness your abilities in the unity framework.Last Note: I recommend getting some where quiet with the book as when any book & just read it, If you can not be in front of a computer none stop , this is also a great book to travel with.Well worth the price IMO.
Amazon Verified review Amazon
Jennifer Nov 08, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is what got me over the "hump" of understanding general programming. It's a great intro into C# scripting with unity. I would highly recommend this book to anyone with no to a very basic understanding of unity and C#. Terry Norton did a great job.The only qualm with it so far is that a few times I was pulling my hair out trying to figure out why an example didn't work or what you expect to happen doesn't. Only to find out a page later that it wasn't supposed to. A little warning would be nice. Maybe bold letters before the example stating that it isn't supposed to work right. Aside from that great book.
Amazon Verified review Amazon
Doug warner May 25, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I think the one thing I liked about this book is how it describes using the state machine to easily translate between scenes and states. I tried doing other video games and as they got bigger and bigger they became extremely difficult to handle. Setting up the unity game engine using a state machine would make building a medium to large scale game at least more manageable. If you are trying to make a simple little game then this would probably still be useful but not as necessary. this book also made me change the way I look at making a video game; I would normally just jump in and start coding with my normal right brain approach. this book seemed to get me to incorporate my left brain to provide a more structured code instead of my usually trial and error approach where I would usually have to go back and re-code and reorganize my files to help make sense of what is going on.Cons: 1.) It sometimes over-explains things. Note: It does do a good job of explaining(for the most part) but does it in a very tedious manner. 2.) It really just explains basic C# and how to begin making a game using a state machine. Which is all I needed (I already knew C and some C++ so chapters 1-4 and 1/2 of chapter 5 was stuff I already knew ), but if you are looking to get a high polished game out of this book then you should look elsewhere. 3.) He sometimes leaves out steps, which was easy enough for me to figure out; but, if you are brand new to programming and/or using Unity then it may be a little difficult to know how resolve these issues.Despite these cons I still gave it a 5 because they were insignificant to me.
Amazon Verified review Amazon
DavidStrife Jun 22, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I bought it with an expectation that there'd be direct examples or explanations of things to carry out in Unity using C#, but actually the book really focuses solely on C#, and puts it in a Unity contextual environment.You won't see any example projects or direct tutorials, but it does show you different implementations of how to execute well known functions and procedures such as limited/numbered loops, and basic programming concepts anyone should know when starting out.A stellar book that basically helped me climb over the final steep part of my learning curve with programming, and finally gave me that "eureka" moment with programming in general, and not just C#.If you've tried to learn programming, but are just finding it a tad bit difficult, and are looking for that "holy grail" book to finally sink it into your head, this book will knock you over to the other side of the fence you're standing on. If you're a programmer but are trying to learn C# specifically for Unity, you'll breeze through this in days.
Amazon Verified review Amazon
Israel S. Dec 01, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good Product
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.