0
0
Unityframework~10 mins

Prefabs and reusable objects 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 instantiate a prefab in Unity.

Unity
GameObject newObject = Instantiate([1]);
Drag options to blanks, or click blank then click option'
AgameObject
Btransform
CVector3.zero
DmyPrefab
Attempts:
3 left
💡 Hint
Common Mistakes
Using a transform or position instead of the prefab variable.
2fill in blank
medium

Complete the code to set the position of the instantiated prefab.

Unity
GameObject newObject = Instantiate(myPrefab, [1], Quaternion.identity);
Drag options to blanks, or click blank then click option'
Atransform.position
Bnew Vector3(1, 1, 1)
CVector3.zero
DgameObject.transform
Attempts:
3 left
💡 Hint
Common Mistakes
Using a transform instead of a Vector3 position.
3fill in blank
hard

Fix the error in the code to properly instantiate a prefab with a parent transform.

Unity
GameObject newObject = Instantiate(myPrefab, [1], Quaternion.identity, parentTransform);
Drag options to blanks, or click blank then click option'
AVector3.zero
Btransform.position
CparentTransform.position
Dnew Vector3(0, 0, 0)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a Transform instead of a Vector3 for position.
4fill in blank
hard

Fill both blanks to create a dictionary of prefab names and their instances only if the prefab name length is greater than 3.

Unity
var instances = prefabs.Where(prefab => [2]).ToDictionary(prefab => [1], prefab => Instantiate(prefab));
Drag options to blanks, or click blank then click option'
Aprefab.name
Bprefab.name.Length > 3
Cprefab.Length > 3
Dprefab.name == "Player"
Attempts:
3 left
💡 Hint
Common Mistakes
Using prefab.Length which is invalid.
Using equality check instead of length comparison.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase prefab names to their instances if the instance is active.

Unity
var activeInstances = prefabs.Where(prefab => [3]).ToDictionary(prefab => [1], prefab => [2]);
Drag options to blanks, or click blank then click option'
Aprefab.name.ToUpper()
BInstantiate(prefab)
CInstantiate(prefab).activeSelf
Dprefab.name
Attempts:
3 left
💡 Hint
Common Mistakes
Using the prefab name without uppercase.
Checking prefab instead of instance active state.