0
0
Unityframework~20 mins

Prefabs and reusable objects in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefab Master
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 prefab instantiation code?

Consider the following Unity C# script snippet that instantiates a prefab multiple times:

public GameObject enemyPrefab;

void Start() {
    for (int i = 0; i < 3; i++) {
        GameObject enemy = Instantiate(enemyPrefab);
        enemy.name = "Enemy_" + i;
        Debug.Log(enemy.name);
    }
}

What will be printed in the Unity console?

Unity
public GameObject enemyPrefab;

void Start() {
    for (int i = 0; i < 3; i++) {
        GameObject enemy = Instantiate(enemyPrefab);
        enemy.name = "Enemy_" + i;
        Debug.Log(enemy.name);
    }
}
AEnemy_0\nEnemy_1\nEnemy_2
BenemyPrefab\nenemyPrefab\nenemyPrefab
CEnemy_1\nEnemy_2\nEnemy_3
DEnemy_0\nEnemy_0\nEnemy_0
Attempts:
2 left
💡 Hint

Look at how the loop index i is used to name each instantiated object.

🧠 Conceptual
intermediate
1:30remaining
Which statement about prefab instances is true?

In Unity, when you create an instance of a prefab in the scene, which of the following is correct?

AChanges made to one instance automatically update the prefab asset and all other instances.
BChanges made to the prefab asset automatically update all instances in the scene.
CInstances cannot be modified individually once created.
DPrefab instances are completely independent and have no link to the original prefab.
Attempts:
2 left
💡 Hint

Think about how prefab assets and instances relate in Unity.

🔧 Debug
advanced
2:00remaining
Why does this prefab instantiation code cause a NullReferenceException?

Examine the following Unity C# code snippet:

public GameObject bulletPrefab;

void Shoot() {
    GameObject bullet = Instantiate(bulletPrefab);
    bullet.GetComponent().AddForce(transform.forward * 500);
}

When running, it throws a NullReferenceException at the GetComponent line. What is the most likely cause?

AThe <code>AddForce</code> method requires a second parameter specifying the force mode.
BThe <code>Rigidbody</code> component is missing from the <code>bulletPrefab</code> prefab.
CThe <code>bulletPrefab</code> variable is not assigned in the Inspector, so it is null.
DThe <code>transform</code> variable is null in this context.
Attempts:
2 left
💡 Hint

Check if the prefab variable is assigned before instantiating.

📝 Syntax
advanced
2:00remaining
Which option correctly instantiates a prefab and sets its parent?

You want to instantiate a prefab and make the new object a child of a specific GameObject called container. Which code snippet is correct?

AGameObject obj = Instantiate(prefab, container.transform, false);
BGameObject obj = Instantiate(prefab, container.transform);
CGameObject obj = Instantiate(prefab, container.transform.position, container.transform.rotation); obj.transform.parent = container.transform;
DGameObject obj = Instantiate(prefab); obj.transform.parent = container.transform;
Attempts:
2 left
💡 Hint

Look for the overload of Instantiate that accepts a parent transform and a boolean for world position.

🚀 Application
expert
2:30remaining
How many unique prefab instances exist after this code runs?

Given the following code snippet in Unity:

public GameObject enemyPrefab;

void SpawnEnemies() {
    GameObject[] enemies = new GameObject[5];
    for (int i = 0; i < 5; i++) {
        enemies[i] = Instantiate(enemyPrefab);
    }
    enemies[2] = enemies[0];
}

After SpawnEnemies() finishes, how many unique GameObjects exist in the enemies array?

A1
B3
C5
D4
Attempts:
2 left
💡 Hint

Consider what happens when you assign enemies[2] = enemies[0].