Tiles placed at positions (0,0), (0,1), (1,0), (1,1), (2,0), (2,1) -> Option A
Quick Check:
Nested loops fill grid area = positions listed [OK]
Hint: Nested loops fill all grid positions in range [OK]
Common Mistakes:
Miscounting loop ranges or tile positions
Assuming only diagonal tiles placed
4. Identify the error in this Unity C# code that tries to fill a 2x2 area with tiles:
for (int x = 0; x <= 2; x++) {
for (int y = 0; y <= 2; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), tile);
}
}
medium
A. The loops should use < instead of <= to avoid infinite loop
B. The loops run too many times, placing tiles in a 3x3 area instead of 2x2
C. The tilemap variable is not initialized
D. The SetTile method is called with wrong parameters
Solution
Step 1: Check loop conditions and iterations
Loops use <= 2, so x and y go from 0 to 2 inclusive, which is 3 iterations each.
Step 2: Calculate total tiles placed
3 x 3 = 9 tiles placed, which is a 3x3 area, not 2x2.
Final Answer:
The loops run too many times, placing tiles in a 3x3 area instead of 2x2 -> Option B
Quick Check:
Loop <= 2 means 3 iterations, not 2 [OK]
Hint: Use < for loop limits to match area size [OK]
Common Mistakes:
Using <= instead of < causing extra iterations
Assuming loops run only 2 times when they run 3
5. You want to create a checkerboard pattern on a 4x4 Tilemap grid using two tiles: tileA and tileB. Which code snippet correctly sets tiles so that adjacent tiles alternate between tileA and tileB?
hard
A. for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), y % 2 == 0 ? tileA : tileB);
}
}
B. for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), x % 2 == 0 ? tileA : tileB);
}
}
C. for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), (x + y) % 2 == 0 ? tileA : tileB);
}
}
D. for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), tileA);
}
}
Solution
Step 1: Understand checkerboard pattern logic
Checkerboard alternates tiles so adjacent tiles differ. Using (x + y) % 2 == 0 alternates tiles diagonally and horizontally.
Step 2: Evaluate each option's tile assignment
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), (x + y) % 2 == 0 ? tileA : tileB);
}
} uses (x + y) % 2 to alternate tileA and tileB correctly. The options using x % 2 or y % 2 alone create stripes by row or column, not checkerboard. for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), tileA);
}
} uses only tileA.
Final Answer:
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
tilemap.SetTile(new Vector3Int(x, y, 0), (x + y) % 2 == 0 ? tileA : tileB);
}
} -> Option C
Quick Check:
Checkerboard = alternate tiles by (x + y) parity [OK]
Hint: Use (x + y) % 2 to alternate tiles in grid [OK]
Common Mistakes:
Alternating only by x or y, not both
Using same tile everywhere
Forgetting to use modulo for pattern
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.