Written By: Saniya Kalamkar
In the last article we briefly went over what components are (Remember, GameObjects are just containers, so we have to attach components that determine the behavior of the GameObject.) We sometimes use scripting to edit or create our own components. However, there are many components that Unity has already provided. We will go over the most important and most used components, but there are many more.
Built in Components:
Light: Pretty self-explanatory. It lights up your screen.
Camera: The position of the camera defines what type of gameplay it will be. For example, if the camera is attached to the player, the gameplay will be in the player's perspective.
Rigidbody: Adding a Rigidbody component to a GameObject allows the GameObject to experience the laws of physics, like gravity.
Colliders: Colliders are invisible and make physical collisions possible. Colliders have the is Trigger property. Colliders that are tagged as a Trigger will not obey physics, and will allow other colliders to pass through. An example of a good use of this property is when you have a player trying to get coins. You would want the player to pass through the coin. In 3D, there are Box Collider, Sphere Collider, and a Capsule Collider. You just pick the collider that most closely resembles your GameObject. For example, a coin would need a Sphere Collider, as it is a sphere shape.
Static Colliders: Static colliders are GameObjects with a collider component, without a Rigidbody component also attached. This means that they will not move in response to collisions. Floors and walls are examples of static colliders.
Dynamic Colliders: Dynamic colliders are GamObjects with a collider component and a Rigidbody component. They do respond to collisions.
Kinematic Rigidbody Colliders: Kinematic Rigidbodies behave similarly to static colliders. They are not affected by physics, but other objects can collide into them. They are controlled only by the script or the Transform component.
In the Inspector window, you can tag GameObjects. Tagging them allows you to identify GameObjects for scripting. For example, you might Tag a GameObject as “Player” or ‘Enemy” or “Coin.” Any word can be used to Tag a GameObject.
Making a GameObject a prefab allows you to save the GameObject’s components, properties, and child GameObjects into an Asset. This means that whenever you want to reuse a GameObject, you should turn it into a prefab. Something that is used often in your gameplay, such as walls, coins, or weapons should be turned into prefabs.
Creating Prefabs: You create a prefab by dragging a Gameobject from the Hierarchy window and into the Project window and becomes a new Asset!
Editing Prefabs: If you change a prefab, all other instances of that prefab are also changed.
Nesting Prefabs: A nested prefab is a prefab that is placed inside another prefab. This is similar to parenting GameObjects. A wheel prefab would be placed inside a car prefab.
Instantiating and Destroying Objects:
You instantiate GameObjects when you need to create new objects during gameplay. An example of this would be bullets from a gun or coins appearing when the player enters a scene. When you want to instantiate an object, prefabs are incredibly useful.
Destroying GameObjects is also very useful. For example, when a player reaches a coin, you would want the coin to be destroyed. When you want to destroy an object, prefabs are also incredibly useful.
To instate an object, we call Instantiate () in the script.
To destroy and object we call Destroy() in the script.