Book Image

Learning C# 7 By Developing Games with Unity 2017 - Third Edition

Book Image

Learning C# 7 By Developing Games with Unity 2017 - Third Edition

Overview of this book

Do you want to learn C# programming by creating fun and interactive games using the latest Unity 2017 platform? If so, look no further; this is the right book for you. Get started with programming C# so you can create 2D and 3D games in Unity. We will walk you through the basics to get you started with C# 7 and its latest features. Then, explore the use of C# 7 and its latest functional programming capabilities to create amazing games with Unity 2017. You will create your first C# script for Unity, add objects into it, and learn how to create game elements with it. Work with the latest functional programming features of C# and leverage them for great game scripting. Throughout the book, you will learn to use the new Unity 2017 2D tool set and create an interactive 2D game with it. You will make enemies appear to challenge your player, and discover some optimization techniques for great game performance. At the end, you will learn how to transform a 2D game into 3D, and you will be able to skill up to become a pro C# programmer with Unity 2017!
Table of Contents (23 chapters)
Title Page
Credits
About the Authors
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Teaching behavior to GameObjects


You have Unity because you want to make a game or something interactive, such as an AR or VR experience. You've filled your game with dumb GameObjects. What you have to do now is be their teacher. You have to teach them everything that they need to know to live in this world of make–believe. This is the part where you have to write down instructions so that your GameObjects can be smarter.

Here's a quote from the Unity Manual:

"The behavior of GameObjects is controlled by the Components that are attached to them... Unity allows you to create your own Components using scripts."

Notice the word behavior. It reminds me of a parent teaching a child proper behavior. This is exactly what we are going to do when we write scripts for our GameObjects: we'll teach them the behaviors we want them to have. The best part is that Unity has provided a long list of all the behaviors that we can give to our GameObjects. This list of behaviors is documented in the Scripting Reference.

This means that we can pick and choose anything that we want a GameObject to do from this list of behaviors. Unity has done all the hard work of programming all of these behaviors for you. All we need to do is use some code to tie into these behaviors. Did you catch that? Unity has already created the behaviors; all that we have to do is supply a bit of C# code to apply these behaviors to our GameObjects. Now, how difficult can it really be since Unity has already done most of the programming?

Using Unity's documentation

When we begin writing scripts, we will be looking at Unity's documentation quite often, so it's beneficial to know how to access the information we need. For an overview of a topic, we'll use the Reference Manual, and for specific coding details and examples, we'll use the Scripting Reference.

There are a number of ways to access the Unity documentation:

  • Through the Unity website at http://docs.unity3d.com/ScriptReference/index.html.
  • Through the Help menu on the top bar. In this way, you can access a local copy of Unity reference as we can see in the following image. This is worth remembering if there are internet connectivity issues:
  • Let's open Scripting Reference now and search for a GameObject:
  • Through the Help menu next to the component name. This will work only for Unity's built-in, standard components.

This is the place where we can find scripting documentation, answers to our questions, and a lot of example code. You might feel a bit lost right now, but don't worry, this is quite normal. The Unity documentation is really easy to use. For the fastest access to relevant information, use Search scripting... in the top–right corner, as shown here:

Note

The whole reason Scripting Reference exists is so that we can look for information as we need it. This will actually make us remember the code that we write over and over, just like our other daily routines and habits. It is a very good idea to take a brief look through the most common Unity objects, such as GameObject, Transform, MonoBehaviour, and Renderer.