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?
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);Count how many unique points you define in the vertices array.
The mesh has exactly 3 vertices defined in the vertices array. The vertexCount property returns the number of vertices, which is 3.
Choose the correct description of the MeshRenderer component's role in Unity.
Think about what makes a mesh visible in the game view.
The MeshRenderer component is responsible for drawing the mesh on the screen. It uses materials and lighting to display the mesh visually. The mesh shape is defined by the Mesh component, not the renderer.
The following code creates a mesh but nothing appears in the scene. What is the most likely reason?
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
Think about what component is needed to make a mesh visible.
Without a MeshRenderer component, the mesh will not be drawn on the screen even if the mesh data is correct. Adding a MeshRenderer will fix the issue.
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
Check the end of each statement in C#.
In C#, each statement must end with a semicolon. The missing semicolon after the triangles array assignment causes a syntax error.
This code modifies the normals of a mesh. What will be the value of mesh.normals[0] after running?
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]);Think about how arrays and structs work in C# when modifying elements.
The code sets the first normal vector to (1, 0, 0). Accessing mesh.normals[0] after this change returns the updated vector.