0
0
Unityframework~30 mins

Mesh and mesh renderer in Unity - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a Simple Colored Triangle with Mesh and MeshRenderer in Unity
📖 Scenario: You are making a simple 3D scene in Unity. You want to create a triangle shape from scratch using code. This triangle will be visible in the scene with a color.
🎯 Goal: Build a Unity script that creates a Mesh with three vertices forming a triangle, assigns it to a MeshFilter, and adds a MeshRenderer with a simple material so the triangle is visible in the scene.
📋 What You'll Learn
Create a Mesh with exactly three vertices forming a triangle
Define the triangle's vertex indices correctly
Add a MeshFilter component and assign the created mesh to it
Add a MeshRenderer component and assign a basic material
Use a GameObject to hold the mesh and renderer
💡 Why This Matters
🌍 Real World
Creating custom 3D shapes programmatically is useful for procedural generation, custom effects, or tools in game development.
💼 Career
Understanding Mesh and MeshRenderer components is essential for Unity developers working on 3D games or simulations.
Progress0 / 4 steps
1
Create a new GameObject and define the triangle vertices
Write code to create a new GameObject called triangleObject. Then create a Mesh called mesh and define its vertices as a Vector3 array with these exact points: (0, 0, 0), (0, 1, 0), and (1, 0, 0).
Unity
Need a hint?

Use new GameObject("Triangle") to create the object. Then create a new Mesh and assign the vertices property with a Vector3 array of three points.

2
Define the triangle indices for the mesh
Add code to set the triangles property of the mesh to an integer array with these exact values: 0, 1, 2. This defines the order of vertices to form the triangle.
Unity
Need a hint?

The triangles array tells Unity which vertices to connect. Use mesh.triangles = new int[] { 0, 1, 2 };.

3
Add MeshFilter component and assign the mesh
Add a MeshFilter component to triangleObject and assign the mesh you created to its mesh property.
Unity
Need a hint?

Use AddComponent<MeshFilter>() on the GameObject, then set its mesh property.

4
Add MeshRenderer and assign a basic material
Add a MeshRenderer component to triangleObject. Then create a new Material using Shader.Find("Standard") and assign it to the material property of the MeshRenderer.
Unity
Need a hint?

Use AddComponent<MeshRenderer>() and create a new Material with the standard shader.