0
0
Unityframework~20 mins

GetComponent usage in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GetComponent 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 this GetComponent call?

Consider a Unity GameObject with a Rigidbody component attached. What will the following code print in the console?

Unity
void Start() {
    Rigidbody rb = GetComponent<Rigidbody>();
    if (rb != null) {
        Debug.Log("Rigidbody found");
    } else {
        Debug.Log("No Rigidbody");
    }
}
ARigidbody found
BNo Rigidbody
CNullReferenceException
DCompilation error
Attempts:
2 left
💡 Hint

Think about whether the Rigidbody component is attached to the same GameObject.

Predict Output
intermediate
2:00remaining
What happens if GetComponent is called for a missing component?

What will this code print if the GameObject does NOT have a BoxCollider component?

Unity
void Start() {
    BoxCollider box = GetComponent<BoxCollider>();
    if (box == null) {
        Debug.Log("BoxCollider missing");
    } else {
        Debug.Log("BoxCollider found");
    }
}
ACompilation error
BBoxCollider found
CNullReferenceException
DBoxCollider missing
Attempts:
2 left
💡 Hint

GetComponent returns null if the component is not found.

Predict Output
advanced
2:00remaining
What is the output when using GetComponent in a child GameObject?

Given this code in a parent GameObject, what will it print if the child has a Light component but the parent does not?

Unity
void Start() {
    Light lightComp = GetComponent<Light>();
    if (lightComp != null) {
        Debug.Log("Light found on parent");
    } else {
        Debug.Log("Light not found on parent");
    }
}
ALight found on parent
BLight not found on parent
CNullReferenceException
DLight found on child
Attempts:
2 left
💡 Hint

GetComponent only searches the GameObject it is called on, not children.

Predict Output
advanced
2:00remaining
What is the output of this GetComponent usage with TryGetComponent?

What will this code print if the GameObject has a Camera component?

Unity
void Start() {
    if (TryGetComponent<Camera>(out Camera cam)) {
        Debug.Log("Camera found");
    } else {
        Debug.Log("Camera missing");
    }
}
ACamera missing
BNullReferenceException
CCamera found
DCompilation error
Attempts:
2 left
💡 Hint

TryGetComponent returns true if the component exists.

🧠 Conceptual
expert
3:00remaining
Which option correctly explains GetComponent behavior with interfaces?

Suppose you have a component that implements an interface IMyInterface. Which statement about GetComponent<IMyInterface>() is true?

AGetComponent&lt;IMyInterface&gt;() returns the component implementing the interface if it exists on the GameObject.
BGetComponent&lt;IMyInterface&gt;() always returns null because interfaces cannot be components.
CGetComponent&lt;IMyInterface&gt;() throws a runtime exception if the interface is not found.
DGetComponent&lt;IMyInterface&gt;() returns all components implementing the interface as a list.
Attempts:
2 left
💡 Hint

Unity supports GetComponent with interfaces implemented by components.