Bird
Raised Fist0
Unityframework~20 mins

Adding and removing components in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Component 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 adding a Rigidbody component?
Consider the following Unity C# code snippet. What will be the output in the console after running this code attached to a GameObject?
Unity
using UnityEngine;

public class TestComponent : MonoBehaviour
{
    void Start()
    {
        Rigidbody rb = gameObject.AddComponent<Rigidbody>();
        Debug.Log(rb != null ? "Rigidbody added" : "Failed to add Rigidbody");
    }
}
AFailed to add Rigidbody
BRigidbody added
CNullReferenceException at Debug.Log
DNo output
Attempts:
2 left
💡 Hint
Adding a component returns the new component instance if successful.
❓ Predict Output
intermediate
2:00remaining
What happens when removing a component?
What will be the output of this Unity C# code snippet when attached to a GameObject that already has a BoxCollider component?
Unity
using UnityEngine;

public class RemoveComponentTest : MonoBehaviour
{
    void Start()
    {
        BoxCollider bc = gameObject.GetComponent<BoxCollider>();
        Destroy(bc);
        Debug.Log(gameObject.GetComponent<BoxCollider>() == null ? "BoxCollider removed" : "BoxCollider still present");
    }
}
ABoxCollider still present
BNo output
CNullReferenceException at Destroy
DBoxCollider removed
Attempts:
2 left
💡 Hint
Destroy removes the component but only after the current frame ends.
🔧 Debug
advanced
2:00remaining
Why does this code throw an error when removing a component?
Examine the code below. It throws a runtime error when trying to remove a component. What is the cause?
Unity
using UnityEngine;

public class RemoveError : MonoBehaviour
{
    void Start()
    {
        Rigidbody rb = gameObject.GetComponent<Rigidbody>();
        Destroy(rb);
        Destroy(rb);
    }
}
AGetComponent<Rigidbody>() returns null causing Destroy(null) error
BDestroy requires a delay parameter
CDestroy cannot be called inside Start method
DDestroying the same component twice causes a NullReferenceException
Attempts:
2 left
💡 Hint
Check what happens after the first Destroy call to the component reference.
🧠 Conceptual
advanced
1:30remaining
Which statement about AddComponent and Destroy is true?
Select the correct statement about adding and removing components in Unity.
AAddComponent immediately adds the component, but Destroy removes it only after the frame ends
BAddComponent and Destroy both immediately change the GameObject's components list
CDestroy immediately removes the component, but AddComponent delays adding until next frame
DAddComponent and Destroy both delay their operations until the next frame
Attempts:
2 left
💡 Hint
Think about when components become available or removed in the frame lifecycle.
❓ Predict Output
expert
2:30remaining
What is the output after adding and removing components in sequence?
What will be printed to the console after running this Unity C# script attached to a GameObject with no Rigidbody initially?
Unity
using UnityEngine;

public class AddRemoveSequence : MonoBehaviour
{
    void Start()
    {
        Rigidbody rb = gameObject.AddComponent<Rigidbody>();
        Debug.Log(rb != null ? "Added" : "Not added");
        Destroy(rb);
        Rigidbody rbCheck = gameObject.GetComponent<Rigidbody>();
        Debug.Log(rbCheck != null ? "Still there" : "Removed");
    }
}
ANot added\nRemoved
BAdded\nRemoved
CAdded\nStill there
DNot added\nStill there
Attempts:
2 left
💡 Hint
Remember when Destroy actually removes the component.

Practice

(1/5)
1. What does the AddComponent<Type>() method do in Unity?
easy
A. Disables a GameObject temporarily
B. Adds a new component of the specified type to a GameObject
C. Removes a component from a GameObject
D. Creates a new GameObject in the scene

Solution

  1. Step 1: Recall the purpose of AddComponent<Type>()

    This method adds a new component of the specified type to the GameObject it is called on.
  2. Step 2: Rule out distractor options

    Removing components uses Destroy(), disabling is different, and creating GameObjects uses other methods.
  3. Final Answer:

    Adds a new component of the specified type to a GameObject -> Option B
  4. Quick Check:

    AddComponent<Type>() = Add component [OK]
Hint: AddComponent adds, Destroy removes components [OK]
Common Mistakes:
  • Confusing AddComponent with Destroy
  • Thinking AddComponent disables GameObjects
  • Mixing up component addition with GameObject creation
2. Which of the following is the correct syntax to add a Rigidbody component to a GameObject named player?
easy
A. player.AddComponent(Rigidbody);
B. player.AddComponent<Rigidbody>();
C. player.AddComponent<Rigidbody>();
D. player.AddComponent<Rigidbody>;

Solution

  1. Step 1: Recall correct generic syntax

    AddComponent uses generic syntax with angle brackets and parentheses: AddComponent<Type>()
  2. Step 2: Inspect syntax of each option

    player.AddComponent<Rigidbody>(); is HTML-escaped but in actual code should be player.AddComponent<Rigidbody>(); which is correct syntax; others have syntax errors or missing parentheses.
  3. Final Answer:

    player.AddComponent<Rigidbody>(); -> Option C
  4. Quick Check:

    Correct generic syntax = player.AddComponent<Rigidbody>(); [OK]
Hint: Use angle brackets and parentheses: AddComponent<Type>() [OK]
Common Mistakes:
  • Omitting parentheses after AddComponent
  • Using parentheses instead of angle brackets
  • Missing semicolon or wrong brackets
3. What will happen when the following code runs?
var enemy = new GameObject("Enemy");
var rb = enemy.AddComponent<Rigidbody>();
Destroy(rb);
medium
A. The enemy GameObject is destroyed
B. The Rigidbody component is added and remains on the enemy GameObject
C. The code causes a compile-time error
D. The Rigidbody component is added and then immediately removed from the enemy GameObject

Solution

  1. Step 1: Analyze AddComponent call

    A Rigidbody component is added to the enemy GameObject and stored in variable rb.
  2. Step 2: Analyze Destroy call

    Destroy(rb) removes the Rigidbody component, not the GameObject.
  3. Final Answer:

    The Rigidbody component is added and then immediately removed from the enemy GameObject -> Option D
  4. Quick Check:

    AddComponent + Destroy(component) = component removed [OK]
Hint: Destroy(component) removes that component, not the GameObject [OK]
Common Mistakes:
  • Thinking Destroy(rb) deletes the whole GameObject
  • Assuming component stays after Destroy
  • Confusing compile error with runtime behavior
4. Identify the error in this code snippet that tries to remove a BoxCollider component:
var obj = new GameObject();
obj.AddComponent<BoxCollider>();
Destroy(obj.GetComponent<BoxCollider>);
medium
A. GetComponent<BoxCollider> is missing parentheses
B. AddComponent<BoxCollider>() is incorrect syntax
C. Destroy cannot be used on components
D. GameObject cannot have BoxCollider components

Solution

  1. Step 1: Check GetComponent usage

    GetComponent<BoxCollider> is a method and requires parentheses to call it.
  2. Step 2: Verify other parts

    AddComponent syntax is correct, Destroy works on components, and GameObjects can have BoxCollider.
  3. Final Answer:

    GetComponent<BoxCollider> is missing parentheses -> Option A
  4. Quick Check:

    Method calls need parentheses [OK]
Hint: Always add () when calling GetComponent [OK]
Common Mistakes:
  • Forgetting parentheses on GetComponent
  • Thinking Destroy can't remove components
  • Confusing component types allowed on GameObjects
5. You want to add a Light component to a GameObject only if it doesn't already have one, and remove it later if a condition is met. Which code snippet correctly does this?
hard
A. if (obj.GetComponent<Light>() == null) { obj.AddComponent<Light>(); } if (shouldRemove) { Destroy(obj.GetComponent<Light>()); }
B. obj.AddComponent<Light>(); if (shouldRemove) { Destroy(obj.GetComponent<Light>); }
C. if (obj.GetComponent<Light> == null) { obj.AddComponent<Light>(); } if (shouldRemove) { Destroy(obj.GetComponent<Light>()); }
D. if (obj.GetComponent<Light>() != null) { obj.AddComponent<Light>(); } if (shouldRemove) { Destroy(obj.GetComponent<Light>()); }

Solution

  1. Step 1: Check condition to add Light

    if (obj.GetComponent<Light>() == null) { obj.AddComponent<Light>(); } if (shouldRemove) { Destroy(obj.GetComponent<Light>()); } correctly checks if Light component is missing (null) before adding it.
  2. Step 2: Check removal condition

    if (shouldRemove) { Destroy(obj.GetComponent<Light>()); } calls Destroy on the Light component with parentheses, correctly removing it if shouldRemove is true.
  3. Step 3: Examine flaws in other options

    obj.AddComponent<Light>(); if (shouldRemove) { Destroy(obj.GetComponent<Light>); } adds Light without checking and misses parentheses in Destroy call. if (obj.GetComponent<Light> == null) { obj.AddComponent<Light>(); } if (shouldRemove) { Destroy(obj.GetComponent<Light>()); } misses parentheses in GetComponent check. if (obj.GetComponent<Light>() != null) { obj.AddComponent<Light>(); } if (shouldRemove) { Destroy(obj.GetComponent<Light>()); } adds Light if it already exists, which is wrong.
  4. Final Answer:

    if (obj.GetComponent<Light>() == null) { obj.AddComponent<Light>(); } if (shouldRemove) { Destroy(obj.GetComponent<Light>()); } -> Option A
  5. Quick Check:

    Check null before add, use Destroy(component) with () [OK]
Hint: Check null before adding; call Destroy with parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on GetComponent or Destroy
  • Adding component without checking if it exists
  • Removing component without calling method properly
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