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 5.x
Learning C# by Developing Games with Unity 5.x

Learning C# by Developing Games with Unity 5.x: Develop your first interactive 2D platformer game by learning the fundamentals of C# , Second Edition

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.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 5.x

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...

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...

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...

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...

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...

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...

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...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Get to grips with the fundamentals of scripting in C# with Unity
  • Create an awesome, 2D platformer game from scratch using the principles of object-oriented programming and coding in C#
  • This is a step-by-step guide to learn the fundamentals of C# scripting to develop GameObjects and master the basics of the new UI system in Unity

Description

Unity is a cross-platform game engine that is used to develop 2D and 3D video games. Unity 5 is the latest version, released in March 2015, and adds a real-time global illumination to the games, and its powerful new features help to improve a game’s efficiency. This book will get you started with programming behaviors in C# so you can create 2D games in Unity. You will begin by installing Unity and learning about its features, followed by creating a C# script. We will then deal with topics such as unity scripting for you to understand how codes work so you can create and use C# variables and methods. Moving forward, you will find out how to create, store, and retrieve data from collection of objects. You will also develop an understanding of loops and their use, and you’ll perform object-oriented programming. This will help you to turn your idea into a ready-to-code project and set up a Unity project for production. Finally, you will discover how to create the GameManager class to manage the game play loop, generate game levels, and develop a simple UI for the game. By the end of this book, you will have mastered the art of applying C# in Unity.

Who is this book for?

The book is targeted at beginner level Unity developers with no programming experience. If you are a Unity developer and you wish to learn how to write C# scripts and code by creating games, then this book is for you.

What you will learn

  • Understand the fundamentals of variables, methods, and code syntax in C#
  • Get to know about techniques to turn your game idea into working project
  • Use loops and collections efficiently in Unity to reduce the amount of code
  • Develop a game using the object-oriented programming principles
  • Generate infinite levels for your game
  • Create and code a good-looking functional UI system for your game
  • Publish and share your game with users

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 31, 2016
Length: 230 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785287855
Vendor :
Unity Technologies
Languages :
Concepts :
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 : Mar 31, 2016
Length: 230 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785287855
Vendor :
Unity Technologies
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 124.97
Learning C# by Developing Games with Unity 5.x
€36.99
Unity 5.x Cookbook
€45.99
Unity 5.x By Example
€41.99
Total 124.97 Stars icon
Banner background image

Table of Contents

14 Chapters
1. Discovering Your Hidden Scripting Skills and Getting Your Environment Ready 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. Lists, Arrays, and Dictionaries Chevron down icon Chevron up icon
6. Loops Chevron down icon Chevron up icon
7. Object, a Container with Variables and Methods Chevron down icon Chevron up icon
8. Let's Make a Game! – From Idea to Development Chevron down icon Chevron up icon
9. Starting Your First Game Chevron down icon Chevron up icon
10. Writing GameManager Chevron down icon Chevron up icon
11. The Game Level Chevron down icon Chevron up icon
12. The User Interface Chevron down icon Chevron up icon
13. Collectables — What Next? 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
(30 Ratings)
5 star 50%
4 star 16.7%
3 star 10%
2 star 6.7%
1 star 16.7%
Filter icon Filter
Top Reviews

Filter reviews by




pierre parenteau Apr 23, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
tres bon
Amazon Verified review Amazon
Jon Jul 16, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very Instructional, helps you to easily learn the basics of C# on Unity.
Amazon Verified review Amazon
Amazon Customer Jun 30, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very helpful in getting started with Unity.Great for those who are brand new to programming.C# is a great starting language.
Amazon Verified review Amazon
Jaime Sierra Apr 19, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is perfect for learn Unity 5.X step by step, sometimes the issues are complicated but the author relaxes and gives encouragement to follow.The first part of the book is a brief and brilliant summary of important aspects to consider for both novices and people who know another programming language.In addition to download the code they have no problems, so the study flows to the rhythm you want.In short, you note 150% .... very good job !!!
Amazon Verified review Amazon
Kate May 27, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very good book for everyone who wants to learn about C# and Unity. Every aspect covered is explained very well and step by step instructions allow people without any programming knowledge create an awesome game from scratch.Highly recommended!!!
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.