0
0
Unityframework~20 mins

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

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