Game Development Reference
In-Depth Information
At the top of the page is the name of the class, GameObject. As you become more familiar with
scripting, you will see both GameObject and gameObject. If the first character is capitalized, it
refers to the class. If not, it will be referring to a specific instance of the class, e.g., a particular
gameObject.
The next line states the Namespace as UnityEngine. If you remember, when you first created the
script, it came with 'using UnityEngine' at the top of the page. A namespace is a type of shortcut to
tell the script where to find things, or more specifically which one it is referring to. An example could
be two people named John Smith. The first John Smith lives on Baker Street. The second one lives
on Maple Avenue. By declaring a namespace of BakerStreet, you are saying that every time you refer
to johnSmith, you are referring to the one on Baker Street, it implies BakerStreet.johnSmith. So if you
need to refer to the other person, you must explicitly say MapleAvenue.johnSmith. In C#, the period
denotes inheritance and is called dot notation. The part following could be a sub-class, a property,
or a function available for the preceding part.
Let's skip the Parent class for a minute and look at the description, GameObject is the “Base class
for all entities in Unity scenes.” This means that every object in the Hierarchy is a gameObject, an
instance of the class, GameObject.
Going back up to Parent class, that line tells you what class GameObject inherits from, Object
5.
Click on the Object link.
This time, the description says “Base class for all objects Unity can reference.” This includes things
that are not put in the scene (Hierarchy view), such as window size, inputs, ambient light, and pretty
much any other Render or Project settings that are accessed through the Edit menu. The Object
class has only a few variables and functions, but you will use a few of them on a regular basis. Of
note are the name variable and the Destroy and Instantiate functions. Because GameObject inherits
from Object, you can find the selected instance of its name, destroy the current instance of it, or
instantiate a new instance of it.
6.
Take the browser back to the GameObject class.
7.
Note the large number of variables the GameObject class includes.
Any variable that has “attached” in its description is referring to a component on the gameObject
that can be easily accessed by your scripts. The newer entry for Collider2D is more descriptive:
“The Collider2D component attached to this object.” So if you see a component in this list, it is
automatically a type. Let's add a new variable for a Camera component. Component types, because
they are classes, are always capitalized and will turn blue in the script editor.
1.
Add the following new variable to your script, beneath the others:
public Camera theView;
2.
Save the script, and check out the new parameter on the bench in the
Inspector.
As you probably anticipated, this one is expecting a gameObject with a Camera component. The
only object in the scene with a Camera component is the Main Camera gameObject. If you try to
drag any other gameObject onto the The View field, it will not let you drop it.
 
Search WWH ::




Custom Search