0
0
Unityframework~5 mins

GetComponent usage in Unity - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does GetComponent<T>() do in Unity?
It finds and returns a component of type T attached to the same GameObject. If the component is not found, it returns null.
Click to reveal answer
beginner
How do you use GetComponent to access a Rigidbody component in a script?
You write Rigidbody rb = GetComponent<Rigidbody>(); to get the Rigidbody attached to the GameObject.
Click to reveal answer
beginner
What happens if you call GetComponent for a component that does not exist on the GameObject?
The method returns null. You should check for null before using the component to avoid errors.
Click to reveal answer
intermediate
Can GetComponent find components on child GameObjects?
No, GetComponent only searches the GameObject it is called on. To find components on children, use GetComponentInChildren<T>().
Click to reveal answer
intermediate
Why should you avoid calling GetComponent repeatedly in Update()?
Because GetComponent is a bit slow and can hurt performance if called every frame. Instead, cache the component in Start() or Awake().
Click to reveal answer
What does GetComponent<Collider>() return if the GameObject has no Collider?
AThe first Collider found in the scene
BA new Collider
CAn error
Dnull
Which method finds a component on child GameObjects?
AGetComponent&lt;T&gt;()
BGetComponentInChildren&lt;T&gt;()
CFindObjectOfType&lt;T&gt;()
DGetComponentInParent&lt;T&gt;()
Where is the best place to call GetComponent to improve performance?
AInside <code>OnGUI()</code>
BInside <code>Update()</code>
CInside <code>Start()</code> or <code>Awake()</code>
DInside <code>FixedUpdate()</code>
What type of value does GetComponent<T>() return?
AA reference to the component or null
BA copy of the component
CA boolean indicating if the component exists
DAn integer ID of the component
If you want to get a component from the parent GameObject, which method should you use?
AGetComponentInParent&lt;T&gt;()
BGetComponentInChildren&lt;T&gt;()
CGetComponent&lt;T&gt;()
DFindObjectOfType&lt;T&gt;()
Explain how and why you would use GetComponent<T>() in a Unity script.
Think about how scripts talk to each other on the same object.
You got /4 concepts.
    Describe the difference between GetComponent<T>(), GetComponentInChildren<T>(), and GetComponentInParent<T>().
    Consider the GameObject hierarchy and where components might be.
    You got /3 concepts.