0
0
Unityframework~10 mins

GetComponent usage in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the Rigidbody component attached to the GameObject.

Unity
Rigidbody rb = gameObject.[1]<Rigidbody>();
Drag options to blanks, or click blank then click option'
AAddComponent
BGetComponent
CFindComponent
DGetChild
Attempts:
3 left
💡 Hint
Common Mistakes
Using AddComponent instead of GetComponent, which adds a new component instead of fetching.
Using FindComponent which does not exist.
2fill in blank
medium

Complete the code to get the AudioSource component from the current GameObject.

Unity
AudioSource audio = [1].GetComponent<AudioSource>();
Drag options to blanks, or click blank then click option'
AgameObject
Bthis
Ctransform
DCamera
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' which refers to the script instance, not the GameObject.
Using 'transform' which is a component, not the GameObject itself.
3fill in blank
hard

Fix the error in the code to correctly get the Collider component.

Unity
Collider col = gameObject.[1]<Collider>();
Drag options to blanks, or click blank then click option'
AGetComponent<Collider>
BGetComponent<Collider>()
CGetComponent()
DGetComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the generic type inside the blank, causing syntax errors.
Including parentheses inside the blank.
4fill in blank
hard

Fill both blanks to get the SpriteRenderer component from the child GameObject.

Unity
SpriteRenderer sr = transform.[1](0).gameObject.[2]<SpriteRenderer>();
Drag options to blanks, or click blank then click option'
AGetChild
BGetComponent
CFindChild
DGetParent
Attempts:
3 left
💡 Hint
Common Mistakes
Using FindChild which is deprecated or does not exist.
Using GetParent which gets the parent, not the child.
5fill in blank
hard

Fill all three blanks to get the Light component from the parent GameObject's first child.

Unity
Light lightComp = transform.[1]().[2](0).gameObject.[3]<Light>();
Drag options to blanks, or click blank then click option'
AGetParent
BGetChild
CGetComponent
DFind
Attempts:
3 left
💡 Hint
Common Mistakes
Using Find which searches the whole scene and is not a Transform method.
Mixing up GetParent and GetChild order.