What is a GameObject in Unity?
GameObjects are central to the development of most apps made in Unity. So what is a GameObject?
GameObjects are the main objects in Unity. They are containers that can represent characters, lighting, cameras, scenery, and more. You can add Components to GameObjects to increase their functionality.
Because GameObjects are such an important topic in Unity, you should know some basic information about them and how to use them. This will help you to better understand how to create an manipulate a scene in Unity.
How Do I Create a GameObject in Unity?
There are several ways you can create a GameObject in Unity. They are both very simple to do. One of these ways is to use the hierarchy window.
- Right-click somewhere inside the hierarchy window.
- Select “Create Empty”
Another way to create a GameObject is by using the GameObject menu.
- Select the GameObject button in the main menu
- Select “Create Empty”
As you can see, once you create a GameObject it appears in the Hierarchy Window. You’ll also notice that if you look in either the Scene View or the Game View, nothing is on the screen to represent the appearance of the GameObject. That is because you created an empty GameObject. It has no form yet.
The only part of this GameObject is the Transform Component that is attached to it. All GameObjects need a Transform Component attached to them. Left-click on the GameObject’s name in the Hierarchy Window, and look at the Inspector Window. You should see something similar to the image below.
The Transform Component assigns a the position, rotation, and scale of the GameObject. Without it, there would be no reference to the GameObject being in the scene at all. GameObjects are required to have them.
What is the Difference Between a GameObject and a Component?
GameObjects are the building blocks of everything in a Scene. Now think of Components as the building blocks of GameObjects. These building blocks add functionality to GameObjects. Right now our GameObject has no shape or color. Let’s add components to allow someone to see it in the scene.
Let’s turn our empty GameObject into a cube. We need two Components to do this. The first is a Mesh Filter. Do the following below:
- Select the GameObject in the Hierarchy Window.
- In the Inspector Window, select the “Add Component” button.
- In the search box, type in “Mesh Filter”, and select the Mesh Filter Component. This will attach it to your GameObject.
Look at the Mesh Filter Component. You’ll see that it accepts a mesh. So add a cube mesh to the Component.
- Select the little circle button on the right.
- Then double-click on the Cube. If you don’t see the cube or other meshes, make sure the Assets tab is selected and not the Scene tab.
Notice, you still do not see a cube in the Scene. You still need another component to render the scene onto your screen.
- Select the “Add Component” button.
- In the search box, type in “Mesh Renderer”, and select the Mesh Renderer Component.
As soon as you do you will see your cube look something like this in the Scene View. You can now scene
Change the Color of a GameObject
Now that you have a GameObject showing in the scene, how can you change the color?
You can add a Material to the GameObject to change the color. To do this, follow the steps below:
- Right-Click in the Project Window
- Select “Create”
- Select “Material”
After you select the Material in the Project Window, in the Inspector Window you can do the following to change the color:
- Click on the color selector box with the little dropper.
- This will open the color editor where you can select the color. Select the color you want.
- Click on the ‘X’ button at the top right of the box to close it after selecting a color.
Finally, you can add the Material to your GameObject by dragging it onto the GameObject from the Project Window to the Hierarchy Window.
Your GameObject will now be the color that you selected.
There are so many things you can do with GameObjects. Remember, they make up everything you see in a game. I’m sure you’d like to know more about GameObjects. Check an introduction on moving a GameObject here.