0
0
Unityframework~20 mins

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

Choose your learning style9 modes available
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.