Manual     Reference     Scripting   
  
Unity Manual > User Guide > Building Scenes > GameObjects   

GameObjects

GameObjects are the most important objects in Unity. It is very important to understand what a GameObject is, and how it can be used. This page will explain all that for you.

What are GameObjects?

Every object in your game is a GameObject. However, GameObjects don't do anything on their own. They need special properties before they can become a character, an environment, or a special effect. But every one of these objects does so many different things. If every object is a GameObject, how do we differentiate an interactive power-up object from a static room? What makes these GameObjects different from each other?

The answer to this question is that GameObjects are containers. They are empty boxes which can hold the different pieces that make up a lightmapped island or a physics-driven car. So to really understand GameObjects, you have to understand these pieces; they are called Components. Depending on what kind of object you want to create, you will add different combinations of Components to the GameObject. Think of a GameObject as an empty cooking pot, and Components as different ingredients that make up your recipe of gameplay. You can read more about Components here, but we recommend you finish reading this page first.

The GameObject-Component Relationship

We now know that a GameObject contains Components. We'll explore this relationship by discussing the most common Component -- the Transform Component. With any Unity Scene open, create a new GameObject (using Shift-Control-N on Windows or Shift-Command-N on Mac), select it and take a look at the Inspector.


The Inspector of an Empty GameObject

Notice that an empty GameObject still contains a Name, a Tag, and a Layer. Every GameObject also contains a Transform Component. It is impossible to create a GameObject in Unity without a Transform Component. All of the GameObject's Transform properties are enabled by its use of the Transform Component. The Transform Component just happens to be critical to all GameObjects, so each GameObject has one. But GameObjects can contain other Components as well.


The Main Camera, added to each scene by default

Taking a look at the Main Camera GameObject, we can see that it contains a different collection of Components. Specifically, a Camera Component, a GUILayer, a Flare Layer, and an Audio Listener. All of these Components provide additional functionality to the GameObject. Without them, there would be nothing rendering the graphics of the game for the person playing! Rigidbodies, Colliders, Particles, and Audio are all different Components (or combinations thereof) that can be added to any given GameObject.

The Transform Component

The Transform Component is one of the most important Components. It defines the GameObject's position, rotation, and scale in the game world/Scene View. If a GameObject did not have a Transform Component, it would be nothing more than some information in the computer's memory. It effectively would not exist in the world. The Transform Component also enables a concept called Parenting, which is utilized through the Unity Editor and is a critical part of working with GameObjects. If you would like to learn more about the Transform Component and Parenting, please read the Transform Component Reference page.

The GameObject-Script Relationship

When you create a script and and attach it to a GameObject, the script appears in the GameObject's Inspector just like a Component. This is because scripts become Components when they are saved. In technical terms, a script compiles as a type of Component, and is treated like any other Component by the Unity engine.

Any public variables you declare in your script will appear editable or linkable in the Inspector of their GameObject. When you are writing a script, you can directly access any member of the GameObject class. You can see a list of all the GameObject class members here. If any of the indicated classes are attached to the GameObject as a Component, you can access that Component directly through the script by simply typing the member name. For example, typing transform is equivalent to gameObject.transform. The gameObject is assumed by the compiler, unless you specifically reference a different GameObject.

Typing this will be accessing the script Component that you are writing. Typing this.gameObject is referring to the GameObject that the script is attached to. You can access the same GameObject by simply typing gameObject. Logically, typing this.transform is the same as typing transform. If you want to access a Component that is not included as a GameObject member, you have to use gameObject.GetComponent() which is explained on the next page.