Now we have a script we need to attach it to a GameObject to work. I briefly covered GameObjects in Basics of Unity and C# – Introduction to Unity, however in this part I will go into much more detail about them.

A GameObject is a container for different components. Components are things that do something. All the scripts you create and add to objects are components. Unity has many different types of built in GameObjects, such as Cameras, Lights, Basic Shapes etc. The only differences between these GameObjects is they all have different components attached to them which give them their function.

The only component all GameObjects have in common is the Transform component. This component controls where the Object is in the scene, its rotation and how large or small it is (scale).

There is a type of GameObject called an “Empty” GameObject. This GameObject only has a transform component attached to it. This is very useful as you can craft your own GameObjects from scratch, hold scripts which dont really belong on other objects or have them as a parent to other objects to help keep track of similar things in the scene. This is the type of GameObject we will create now.

Open Unity. Right click in an empty space in the hierarchy window. From this menu select Create Empty.

Basics_AddGO001

A new GameObject should appear in the Hierarchy called GameObject. We can easily change its name by clicking on the object and selecting it’s name in the inspector. Change the name to Player and Press enter to save the change.

Basics_AddGO003
Object Appears
Basics_AddGO002
Object Called GameObject
Basics_AddGO004
Object Name Changed to Player

Also check its location in the scene. If it is not at (0, 0, 0) change it now by either entering 0 in each axis box or selecting the gear in the upper right corner of the transform component and selecting reset.

Basics_AddGO005.JPG
Reset Position

Now we can attach our script to it by first clicking on our new object and then dragging the script from our Project window to the Inspector OR by clicking the add component button and searching for MyScript before selecting it from the list.

Basics_AddGO006
Adding Our Script with the Add Component Button

We have now attached our script to the object.

In the next part we will look at public and private variables.