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
Arrow up icon
GO TO TOP
Learning C# 7 By Developing Games with Unity 2017

You're reading from   Learning C# 7 By Developing Games with Unity 2017 Learn C# Programming by building fun and interactive games with Unity

Arrow left icon
Product type Paperback
Published in Dec 2017
Publisher Packt
ISBN-13 9781788478922
Length 290 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Micael DaGraça Micael DaGraça
Author Profile Icon Micael DaGraça
Micael DaGraça
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Discovering Your Hidden Scripting Skills and Getting Your Environment Ready FREE CHAPTER 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 14. Enemies 15. Audio, 3D Games, and Export

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:

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.
You have been reading a chapter from
Learning C# 7 By Developing Games with Unity 2017 - Third Edition
Published in: Dec 2017
Publisher: Packt
ISBN-13: 9781788478922
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 $19.99/month. Cancel anytime
Banner background image