Teaching behavior to GameObjects
You have Unity because you want to make a game or something interactive. 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 that 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. This is worth remembering if there are Internet connectivity issues:
- Through the Help menu next to the component name. This will work only for Unity's built-in, standard components.
Let's open Scripting Reference now and search for a GameObject
. 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 fastest access to relevant information, use Search scripting... in the top-right corner, as shown here:
Do I need to know all that?
Actually, no. 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
Renderer