Components
- Game Development course
- Beginner
- Lesson 20/83
- 4 min
Learn how to add components like Transform and Mesh Renderer.
Components
Components are modular pieces that define what a GameObject actually does. For example, MeshRenderer renders the 3D model on screen, while AudioSource plays audio.
// MeshRenderer draws the object visual on screen
// Transform determines its positionThanks to Unity's component-based architecture, you can build complex behaviors by combining different components.
Adding and accessing components
A new component can be added to an object using the Add Component button in the Inspector window. In code, AddComponent<T>() adds a component, and GetComponent<T>() accesses an existing one.
// Adding and retrieving components via code
Rigidbody rb = gameObject.AddComponent<Rigidbody>();
Transform tr = gameObject.GetComponent<Transform>();This structure allows objects to gain dynamic capabilities at runtime.
Practice Print component name
Select the "MeshRenderer" component from the simulated component list and print it using Console.WriteLine as Component: MeshRenderer.
Starter code
using System;
class Program
{
static void Main()
{
// MeshRenderer bileşenini yazdır
}
}Sample solution
using System;
class Program
{
static void Main()
{
string componentName = "MeshRenderer";
Console.WriteLine("Component: " + componentName);
}
}Expected output
Component: MeshRenderer
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which component is required to visually draw an object's 3D geometry on screen?
- SceneManager
- AudioListener
- MeshRenderer Correct answer
- RigidBodyOnly
Explanation: MeshRenderer is the rendering component that takes geometry from MeshFilter and draws it in the scene.
2 Access component via code
MeshRenderer renderer = gameObject.____<MeshRenderer>();
MeshRenderer renderer = gameObject.GetComponent<MeshRenderer>();
Explanation: In Unity, the GetComponent function is used to access a component on the object.
3 Component functions
| Term | Meaning |
|---|---|
Transform |
Holds position, rotation, and scale |
MeshRenderer |
Makes 3D surfaces visible on screen |
AddComponent |
Adds a component to an object at runtime |
GetComponent |
Finds an attached component in code |