A Mesh holds the shape of a 3D object using points and triangles. A Mesh Renderer shows that shape on the screen by drawing it with materials.
0
0
Mesh and mesh renderer in Unity
Introduction
When you want to create or change the shape of a 3D object in your game.
When you need to display a custom 3D model that you made or imported.
When you want to control how a 3D object looks by changing its material or texture.
When you want to optimize performance by combining or modifying meshes at runtime.
When you want to create procedural objects like terrain, characters, or effects.
Syntax
Unity
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] { ... };
mesh.triangles = new int[] { ... };
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.material = someMaterial;The Mesh stores vertices (points) and triangles (which connect points).
The MeshRenderer draws the mesh using a material that controls color and texture.
Examples
This creates a simple square shape using 4 points and 2 triangles.
Unity
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] {
new Vector3(0, 0, 0),
new Vector3(0, 1, 0),
new Vector3(1, 1, 0),
new Vector3(1, 0, 0)
};
mesh.triangles = new int[] { 0, 1, 2, 0, 2, 3 };This adds a Mesh Renderer to the object and sets a basic material so it can be seen.
Unity
MeshRenderer renderer = gameObject.AddComponent<MeshRenderer>();
renderer.material = new Material(Shader.Find("Standard"));Sample Program
This script creates a square mesh and shows it on the object using a Mesh Filter and Mesh Renderer with a standard material.
Unity
using UnityEngine; public class SimpleMesh : MonoBehaviour { void Start() { Mesh mesh = new Mesh(); Vector3[] vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 0, 0) }; int[] triangles = new int[] { 0, 1, 2, 0, 2, 3 }; mesh.vertices = vertices; mesh.triangles = triangles; MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>(); meshFilter.mesh = mesh; MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>(); meshRenderer.material = new Material(Shader.Find("Standard")); } }
OutputSuccess
Important Notes
Always add a MeshFilter component to hold the mesh before adding a MeshRenderer.
Changing the mesh vertices or triangles after setting them requires calling mesh.RecalculateNormals() for correct lighting.
Materials control how the mesh looks, including color, texture, and shininess.
Summary
A Mesh defines the shape using points and triangles.
A MeshRenderer draws the mesh on the screen with a material.
Use both together to create and show custom 3D objects in Unity.