Bird
Raised Fist0
Unityframework~20 mins

Mesh and mesh renderer in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Mesh Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity C# code snippet?

Consider the following code that creates a simple mesh with 3 vertices forming a triangle. What will be the number of vertices in the mesh after running this code?

Unity
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[] {
    new Vector3(0, 0, 0),
    new Vector3(1, 0, 0),
    new Vector3(0, 1, 0)
};
int[] triangles = new int[] { 0, 1, 2 };
mesh.vertices = vertices;
mesh.triangles = triangles;
Debug.Log(mesh.vertexCount);
A3
B0
C1
D6
Attempts:
2 left
💡 Hint

Count how many unique points you define in the vertices array.

🧠 Conceptual
intermediate
1:30remaining
What does the MeshRenderer component do in Unity?

Choose the correct description of the MeshRenderer component's role in Unity.

AIt handles physics collisions for the mesh.
BIt defines the shape and vertices of the mesh.
CIt renders the mesh on the screen using materials and lighting.
DIt stores the texture data for the mesh.
Attempts:
2 left
💡 Hint

Think about what makes a mesh visible in the game view.

🔧 Debug
advanced
2:00remaining
Why does this mesh not appear in the scene?

The following code creates a mesh but nothing appears in the scene. What is the most likely reason?

Unity
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] {
    new Vector3(0, 0, 0),
    new Vector3(1, 0, 0),
    new Vector3(0, 1, 0)
};
mesh.triangles = new int[] { 0, 1, 2 };

GameObject obj = new GameObject("Triangle");
obj.AddComponent<MeshFilter>().mesh = mesh;
// Missing MeshRenderer component
AThe mesh vertices are all at the same position.
BThe mesh triangles array is empty.
CThe mesh has no vertices defined.
DThe GameObject is missing a MeshRenderer component.
Attempts:
2 left
💡 Hint

Think about what component is needed to make a mesh visible.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this mesh creation code

Which option correctly fixes the syntax error in the following code snippet?

Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] {
    new Vector3(0, 0, 0),
    new Vector3(1, 0, 0),
    new Vector3(0, 1, 0)
};
mesh.triangles = new int[] { 0, 1, 2 } // missing semicolon
AReplace the square brackets [] with parentheses () in the arrays.
BAdd a semicolon at the end of the triangles assignment line.
CRemove the commas between the Vector3 elements.
DChange 'new Vector3' to 'Vector3()' without 'new'.
Attempts:
2 left
💡 Hint

Check the end of each statement in C#.

🚀 Application
expert
2:30remaining
What is the output of this code modifying mesh normals?

This code modifies the normals of a mesh. What will be the value of mesh.normals[0] after running?

Unity
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] {
    new Vector3(0, 0, 0),
    new Vector3(1, 0, 0),
    new Vector3(0, 1, 0)
};
mesh.triangles = new int[] { 0, 1, 2 };
mesh.normals = new Vector3[] {
    new Vector3(0, 0, 1),
    new Vector3(0, 0, 1),
    new Vector3(0, 0, 1)
};
mesh.normals[0] = new Vector3(1, 0, 0);
Debug.Log(mesh.normals[0]);
A(1.0, 0.0, 0.0)
B(0.0, 0.0, 1.0)
C(0.0, 0.0, 0.0)
DNullReferenceException
Attempts:
2 left
💡 Hint

Think about how arrays and structs work in C# when modifying elements.

Practice

(1/5)
1. What is the main role of a Mesh in Unity?
easy
A. It handles user input for 3D objects.
B. It applies textures and colors to a 3D object.
C. It controls the lighting effects on a 3D object.
D. It stores the shape of a 3D object using points and triangles.

Solution

  1. Step 1: Understand the purpose of Mesh

    A Mesh defines the shape of a 3D object by storing vertices (points) and triangles that connect these points.
  2. Step 2: Differentiate from other components

    MeshRenderer is responsible for drawing the mesh with materials, not storing shape data.
  3. Final Answer:

    It stores the shape of a 3D object using points and triangles. -> Option D
  4. Quick Check:

    Mesh = shape data [OK]
Hint: Remember: Mesh = shape, MeshRenderer = drawing [OK]
Common Mistakes:
  • Confusing Mesh with MeshRenderer
  • Thinking Mesh handles textures or lighting
  • Assuming Mesh manages user input
2. Which of the following is the correct way to add a MeshRenderer component to a GameObject in C#?
easy
A. gameObject.AddComponent(MeshRenderer);
B. gameObject.AddComponent<MeshRenderer>;
C. gameObject.AddComponent<MeshRenderer>();
D. gameObject.AddComponent("MeshRenderer");

Solution

  1. Step 1: Recall the syntax for AddComponent

    The correct syntax uses angle brackets with the component type: AddComponent<ComponentType>();
  2. Step 2: Check each option

    gameObject.AddComponent<MeshRenderer>(); uses correct generic syntax with parentheses. Options A and B miss parentheses or use wrong syntax. gameObject.AddComponent("MeshRenderer"); uses a string which is not recommended and causes errors.
  3. Final Answer:

    gameObject.AddComponent<MeshRenderer>(); -> Option C
  4. Quick Check:

    AddComponent syntax = AddComponent<Type>(); [OK]
Hint: Use angle brackets and parentheses: AddComponent<Type>(); [OK]
Common Mistakes:
  • Omitting parentheses after AddComponent
  • Using string names instead of type
  • Missing angle brackets for generic type
3. What will be the output of this code snippet in Unity?
var mesh = new Mesh();
mesh.vertices = new Vector3[] { new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(0,1,0) };
mesh.triangles = new int[] { 0, 1, 2 };
Debug.Log(mesh.vertexCount);
medium
A. 3
B. 0
C. 1
D. Error

Solution

  1. Step 1: Count vertices assigned

    The mesh.vertices array has 3 Vector3 points defined.
  2. Step 2: Check vertexCount property

    mesh.vertexCount returns the number of vertices, which is 3 here.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Vertices count = 3 [OK]
Hint: vertexCount equals length of vertices array [OK]
Common Mistakes:
  • Confusing triangles count with vertex count
  • Expecting zero before assigning vertices
  • Thinking vertexCount counts triangles
4. Identify the error in this code that tries to display a mesh:
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] { new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(0,1,0) };
mesh.triangles = new int[] { 0, 1 };
MeshFilter mf = gameObject.AddComponent<MeshFilter>();
mf.mesh = mesh;
gameObject.AddComponent<MeshRenderer>();
medium
A. Triangles array length is not a multiple of 3.
B. Vertices array is empty.
C. MeshFilter component is missing.
D. MeshRenderer cannot be added at runtime.

Solution

  1. Step 1: Check triangles array length

    Triangles must be defined in groups of 3 indices to form triangles. Here, only 2 indices are given.
  2. Step 2: Verify other components

    Vertices are defined, MeshFilter is added, and MeshRenderer is added correctly.
  3. Final Answer:

    Triangles array length is not a multiple of 3. -> Option A
  4. Quick Check:

    Triangles must be multiples of 3 [OK]
Hint: Triangles array length must be divisible by 3 [OK]
Common Mistakes:
  • Using wrong triangles array length
  • Forgetting to add MeshFilter
  • Thinking MeshRenderer can't be added at runtime
5. You want to create a custom 3D triangle object in Unity with a red color. Which sequence of steps correctly achieves this?
hard
A. Create MeshRenderer only, assign vertices and triangles directly to it, set color to red.
B. Create Mesh with vertices and triangles, add MeshFilter and MeshRenderer, assign a red material to MeshRenderer.
C. Add MeshFilter only, assign vertices and triangles, color is set automatically.
D. Create Mesh, assign color to Mesh, add MeshRenderer without material.

Solution

  1. Step 1: Create and define Mesh

    Define vertices and triangles to shape the triangle.
  2. Step 2: Add MeshFilter and MeshRenderer components

    MeshFilter holds the mesh data; MeshRenderer draws it on screen.
  3. Step 3: Assign a red material to MeshRenderer

    Materials control color and appearance; assigning a red material colors the triangle red.
  4. Final Answer:

    Create Mesh with vertices and triangles, add MeshFilter and MeshRenderer, assign a red material to MeshRenderer. -> Option B
  5. Quick Check:

    Mesh + MeshFilter + MeshRenderer + Material = Colored object [OK]
Hint: MeshFilter holds shape; MeshRenderer draws with material color [OK]
Common Mistakes:
  • Assigning vertices to MeshRenderer instead of MeshFilter
  • Forgetting to assign a material for color
  • Assuming Mesh alone shows the object