0
0
Unityframework~20 mins

Tilemap basics in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tilemap 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 Tilemap cell position code?

Consider this Unity C# code snippet that gets the cell position of a world point in a Tilemap:

Vector3 worldPos = new Vector3(2.5f, 3.7f, 0f);
Vector3Int cellPos = tilemap.WorldToCell(worldPos);
Debug.Log(cellPos);

What will be printed in the console?

Unity
Vector3 worldPos = new Vector3(2.5f, 3.7f, 0f);
Vector3Int cellPos = tilemap.WorldToCell(worldPos);
Debug.Log(cellPos);
A(2, 3, 0)
B(3, 4, 0)
C(2, 4, 0)
D(3, 3, 0)
Attempts:
2 left
💡 Hint

Remember that WorldToCell rounds down to the nearest cell coordinates.

Predict Output
intermediate
2:00remaining
What does this code print when setting a tile?

Given a Tilemap and a TileBase tile, what will this code print?

Vector3Int pos = new Vector3Int(1, 1, 0);
tilemap.SetTile(pos, tile);
TileBase result = tilemap.GetTile(pos);
Debug.Log(result == tile);
Unity
Vector3Int pos = new Vector3Int(1, 1, 0);
tilemap.SetTile(pos, tile);
TileBase result = tilemap.GetTile(pos);
Debug.Log(result == tile);
ATrue
BFalse
CNullReferenceException
DCompilation error
Attempts:
2 left
💡 Hint

SetTile assigns the tile at the position, GetTile returns the tile at that position.

🔧 Debug
advanced
2:00remaining
Why does this Tilemap code throw a NullReferenceException?

Look at this code snippet:

Tilemap tilemap;
void Start() {
    Vector3Int pos = new Vector3Int(0, 0, 0);
    TileBase tile = tilemap.GetTile(pos);
    Debug.Log(tile.name);
}

Why does this code throw a NullReferenceException?

Unity
Tilemap tilemap;
void Start() {
    Vector3Int pos = new Vector3Int(0, 0, 0);
    TileBase tile = tilemap.GetTile(pos);
    Debug.Log(tile.name);
}
Apos is invalid and causes GetTile to fail
Btilemap is not assigned and is null, so calling GetTile causes the exception
Ctile is null because no tile exists at pos, so accessing tile.name causes the exception
DDebug.Log cannot print tile.name because TileBase has no name property
Attempts:
2 left
💡 Hint

Check if the tile exists at the position before accessing its properties.

🧠 Conceptual
advanced
2:00remaining
How does the Tilemap's origin affect tile placement?

In Unity Tilemaps, what happens if you change the Tilemap's origin property?

AThe origin changes the size of each tile in the Tilemap
BThe origin determines the sorting order of tiles when rendered
CThe origin sets the default tile used when no tile is placed
DThe origin shifts the coordinate system, so tile positions are offset accordingly
Attempts:
2 left
💡 Hint

Think about how coordinates relate to tile positions.

Predict Output
expert
3:00remaining
What is the number of tiles after this code runs?

Consider this code that sets tiles in a Tilemap:

for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
        Vector3Int pos = new Vector3Int(x, y, 0);
        if ((x + y) % 2 == 0) {
            tilemap.SetTile(pos, tile);
        }
    }
}
int count = 0;
foreach (var pos in tilemap.cellBounds.allPositionsWithin) {
    if (tilemap.GetTile(pos) != null) count++;
}
Debug.Log(count);

What number will be printed?

Unity
for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
        Vector3Int pos = new Vector3Int(x, y, 0);
        if ((x + y) % 2 == 0) {
            tilemap.SetTile(pos, tile);
        }
    }
}
int count = 0;
foreach (var pos in tilemap.cellBounds.allPositionsWithin) {
    if (tilemap.GetTile(pos) != null) count++;
}
Debug.Log(count);
A4
B5
C9
D6
Attempts:
2 left
💡 Hint

Count how many positions satisfy (x + y) % 2 == 0 in a 3x3 grid.