Using GameObjects and Prefabs– Unity Tutorial
In last tutorial, we discussed about unity interface. In this article, we will see how to use GameObject and prefabs in a unity application.
GameObjects
Every object that you see in the game is a GameObject. It is the fundamental object in unity. You can find all the GameObject in the hierarchy window. GameObjects can have parent child relationship as we saw in last article.
GameObject Components
Alone GameObjects are nothing but the empty container, but when you will add components to the GameObjects, it will add functionalities to them. By default, there may be some components attached to your GameObjects. You can see all the components of a GameObject in the Inspector window.
When you will create a new project in unity then by default there will be two GameObjects in the hierarchy window.
- Main Camera: Controls view of Game Window. What the camera will see, the end user will see the same.
- Directional Light: Provides light in any particular direction
Select any one of the GameObject in the hierarchy window then you can see the attached components in the Inspector window. Select Main Camera and check its components:
Disable Component
To disable any component, uncheck the checkbox located at top left of the component, as shown in above image (1. Uncheck to disable component). If any component is disabled, it will be attached to the GameObject but its functionality will not render in the scene.
Remove Component
To remove any component, click on the icon located at top right of the component, as shown in above image (2. Click to remove component). On Click, following list will be displayed. Click on the Remove Component.
Add Component
Click on the Add Component button to add more components. On click, a component list will be displayed.
You can also drag and drop items from assets folder to inspector window to add component for any GameObject.
Transform Component
Every GameObject will have Transform component. You cannot remove transform component from a GameObject. Transform component is having three rows:
- Position: Change the position of the GameObject.
- Rotation: Change the rotation of GameObject.
- Scale: Change the size of the GameObject.
You can also transform the GameObject using transform tools with mouse buttons.
Change GameObject Properties using Inspector
Inspector window also allows you to disable and rename the GameObject along with other features.
Rename GameObject
Rename the text box located at top of the Inspector and hit Enter to change the name of the GameObject.
Disable GameObject from Hierarchy
Uncheck the top left checkbox in inspector window to disable GameObject from hierarchy window.
Static GameObject
Static GameObject will not move at play time. These are non-moving GameObjects.
Change Tag of GameObject
A tag is simply a string that can be used to reference one or more GameObjects in the scene. You can group a type of GameObject with one tag like all the enemy GameObjects in your game can be tagged as ENEMY tag. Change the tag of a GameObject using Tag dropdown in inspector window.
Change Layer of GameObject
Layer controls the rendering priority of GameObject in the scene. Change the layer of a GameObject using Layer dropdown in inspector window. You can refer my Layer Mask tutorial to know more about Layers.
Create a GameObject
Primitive GameObjects
Primitive GameObjects can be created directly in the unity. Cube, Capsule, Sphere, Cylinder, Plane and quad are the example of primitive 3D GameObjects. You can use these GameObjects for testing purpose or any other requirement. You can check details about them over here.
Right click in hierarchy window to create primitive GameObjects.
You can also click on “Create” button in hierarchy window.
Create Parent-Child Relationship
You can create parent child relationship using GameObjects. Drag and drop one GameObject to another to make it as the child of the another GameObject.
Prefabs
The prefab allows you to store a GameObject with all its components and properties. Once a prefab is created the GameObject can be reused multiple times across the scene and the projects.
Create a Prefab
Create Prefab by selecting Assets -> Create -> Prefab or Right Click to Assets folder in Project window -> Create -> Prefab. Drag and drop a GameObject from hierarchy to this newly created prefab.
You can also create prefab by directly by dragging and dropping GameObject from hierarchy window to the project window.
All the child GameObjects will be automatically included in the prefab.
Add prefab to scene as GameObject
Drag and drop prefab from project window (Asset Folder) to hierarchy to instantiate prefab as the GameObject in the scene. You can instantiate same prefab multiple times in a scene.
Edit Prefab
Select the prefab and you will see all of its properties and components in the Inspector. Modify them upon your requirement, but all the modification will also reflect on already instantiated GameObjects.
Edit Prefab in the Scene
If you edit the GameObject (added by prefab) in the scene, it will not edit the prefab by default. But, if we want to reflect the same change in the prefab then click on apply button in the prefab section of inspector window after selecting the GameObject.
Empty GameObject
You can also create empty GameObject in the hierarchy just like Cube and other GameObjects. It will only have Transform component. It is mostly used to create the parent of the multiple GameObjects.
Hope you get an idea about GameObjects and prefabs in unity application. Post your comments for queries and feedback. Thanks for reading.
Latest posts by Gyanendu Shekhar (see all)
- Using Transparent Material in Unity 3D - February 8, 2021
- Getting started with UI Toolkit : Unity 3D Tutorial - December 30, 2020
- Using Events in Unity 3D - May 2, 2020