Challenge - 5 Problems
Component Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"); } }
Attempts:
2 left
💡 Hint
Adding a component returns the new component instance if successful.
✗ Incorrect
The AddComponent() method adds a Rigidbody component to the GameObject and returns the instance. Since it is successful, the output is "Rigidbody added".
❓ Predict Output
intermediate2: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"); } }
Attempts:
2 left
💡 Hint
Destroy removes the component but only after the current frame ends.
✗ Incorrect
Destroy schedules the component for removal at the end of the frame, so immediately after calling Destroy, the component still exists. Therefore, the output is "BoxCollider still present".
🔧 Debug
advanced2: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);
}
}Attempts:
2 left
💡 Hint
Check what happens after the first Destroy call to the component reference.
✗ Incorrect
After the first Destroy call, the component is marked for removal and effectively null. Calling Destroy again on the same reference causes a NullReferenceException.
🧠 Conceptual
advanced1:30remaining
Which statement about AddComponent and Destroy is true?
Select the correct statement about adding and removing components in Unity.
Attempts:
2 left
💡 Hint
Think about when components become available or removed in the frame lifecycle.
✗ Incorrect
AddComponent adds the component instantly so it can be used immediately. Destroy schedules removal at the end of the frame, so the component still exists until then.
❓ Predict Output
expert2: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"); } }
Attempts:
2 left
💡 Hint
Remember when Destroy actually removes the component.
✗ Incorrect
AddComponent adds the Rigidbody immediately, so "Added" prints. Destroy schedules removal, so immediately after, the Rigidbody still exists, printing "Still there".