Bird
Raised Fist0
Unityframework~20 mins

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

Choose your learning style10 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].

Practice

(1/5)
1. What is the main purpose of a prefab in Unity?
easy
A. To save a game object for reuse multiple times
B. To write scripts for game logic
C. To create animations for characters
D. To design user interface layouts

Solution

  1. Step 1: Understand prefab concept

    A prefab stores a game object with all its components and settings.
  2. Step 2: Identify prefab use

    Prefabs allow easy reuse of objects without recreating them from scratch.
  3. Final Answer:

    To save a game object for reuse multiple times -> Option A
  4. Quick Check:

    Prefab = reusable object [OK]
Hint: Prefabs store objects to reuse easily in your game [OK]
Common Mistakes:
  • Confusing prefabs with scripts
  • Thinking prefabs are only for animations
  • Believing prefabs are UI elements
2. Which of the following is the correct way to create a copy of a prefab in a Unity script?
easy
A. Clone(prefab);
B. Copy(prefab);
C. Duplicate(prefab);
D. Instantiate(prefab);

Solution

  1. Step 1: Recall Unity method for prefab copies

    Unity uses the Instantiate() method to create copies of objects.
  2. Step 2: Check method names

    Only Instantiate() is a valid Unity method for this purpose.
  3. Final Answer:

    Instantiate(prefab); -> Option D
  4. Quick Check:

    Instantiate() creates prefab copies [OK]
Hint: Use Instantiate() to copy prefabs in scripts [OK]
Common Mistakes:
  • Using Copy() which does not exist
  • Trying Clone() or Duplicate() which are invalid
  • Forgetting parentheses after Instantiate
3. What will be the output of this Unity C# code snippet?
public GameObject prefab;
void Start() {
    GameObject obj = Instantiate(prefab);
    Debug.Log(obj.name);
}
medium
A. The name of the prefab with "(Clone)" appended
B. The original prefab name without changes
C. An error because prefab is not assigned
D. Nothing is printed

Solution

  1. Step 1: Understand Instantiate behavior

    Instantiate creates a copy of the prefab and appends "(Clone)" to its name.
  2. Step 2: Analyze Debug.Log output

    Debug.Log prints the name of the new object, which includes "(Clone)".
  3. Final Answer:

    The name of the prefab with "(Clone)" appended -> Option A
  4. Quick Check:

    Instantiate adds "(Clone)" to name [OK]
Hint: Instantiate adds "(Clone)" to copied prefab names [OK]
Common Mistakes:
  • Expecting original prefab name without "(Clone)"
  • Assuming no output is printed
  • Forgetting prefab must be assigned in inspector
4. Identify the error in this Unity C# code that tries to instantiate a prefab:
public GameObject prefab;
void Start() {
    GameObject obj = Instantiate(prefab, new Vector3(0, 0, 0));
}
medium
A. Vector3 cannot be used as a position
B. prefab must be assigned in the inspector
C. Instantiate requires a rotation parameter when using position
D. Instantiate cannot be called in Start()

Solution

  1. Step 1: Check Instantiate method overloads

    Instantiate with position requires both position and rotation parameters.
  2. Step 2: Identify missing rotation argument

    The code misses the rotation argument, causing a compile error.
  3. Final Answer:

    Instantiate requires a rotation parameter when using position -> Option C
  4. Quick Check:

    Instantiate(position) needs rotation too [OK]
Hint: Instantiate with position needs rotation argument [OK]
Common Mistakes:
  • Omitting rotation parameter
  • Assuming Vector3 alone is enough
  • Thinking Instantiate can't be used in Start()
5. You want to create 5 copies of a prefab at different positions along the X-axis, spaced 2 units apart, in Unity. Which code snippet correctly does this?
hard
A. for (int i = 0; i < 5; i++) { Instantiate(prefab); prefab.transform.position = new Vector3(i * 2, 0, 0); }
B. for (int i = 0; i < 5; i++) { Instantiate(prefab, new Vector3(i * 2, 0, 0), Quaternion.identity); }
C. Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity); Instantiate(prefab, new Vector3(1, 0, 0), Quaternion.identity); Instantiate(prefab, new Vector3(2, 0, 0), Quaternion.identity); Instantiate(prefab, new Vector3(3, 0, 0), Quaternion.identity); Instantiate(prefab, new Vector3(4, 0, 0), Quaternion.identity);
D. for (int i = 0; i < 5; i++) { prefab.transform.position = new Vector3(i * 2, 0, 0); Instantiate(prefab); }

Solution

  1. Step 1: Understand correct Instantiate usage with position

    Instantiate(prefab, position, rotation) creates a copy at the given position.
  2. Step 2: Check loop for multiple copies

    for (int i = 0; i < 5; i++) { Instantiate(prefab, new Vector3(i * 2, 0, 0), Quaternion.identity); } uses a loop to create 5 copies spaced by 2 units on X-axis correctly.
  3. Step 3: Identify errors in other options

    The loop that instantiates first then moves prefab results in clones at incorrect positions (0, 0, 2, 4, 6); the manual five Instantiate calls use 1-unit spacing (0,1,2,3,4) instead of 2 units and are repetitive; the loop that moves prefab before instantiating spawns all at (0,0,0) because no position parameter is passed.
  4. Final Answer:

    for (int i = 0; i < 5; i++) { Instantiate(prefab, new Vector3(i * 2, 0, 0), Quaternion.identity); } -> Option B
  5. Quick Check:

    Loop + Instantiate with position = multiple copies [OK]
Hint: Use loop with Instantiate(prefab, position, rotation) [OK]
Common Mistakes:
  • Changing prefab position instead of copy
  • Not providing rotation in Instantiate
  • Writing repetitive Instantiate calls instead of loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard