0
0
Unityframework~10 mins

Async/await 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 declare an async method in Unity.

Unity
public async Task [1]() {
    await Task.Delay(1000);
    Debug.Log("Data loaded");
}
Drag options to blanks, or click blank then click option'
ALoadAsyncData
BLoadDataAsync
CLoadData
DAsyncLoadData
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the method without 'Async' suffix.
Using a name that does not clearly indicate async behavior.
2fill in blank
medium

Complete the code to await a delay of 2 seconds inside an async method.

Unity
await [1](2000);
Drag options to blanks, or click blank then click option'
AThread.Sleep
BWaitForSeconds
CTask.Delay
DCoroutine.Wait
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.Sleep which blocks the thread.
Using Unity's WaitForSeconds which is for coroutines, not async/await.
3fill in blank
hard

Fix the error in the async method signature to correctly return a Task.

Unity
public async [1] LoadSceneAsync() {
    await SceneManager.LoadSceneAsync("MainScene").completed;
}
Drag options to blanks, or click blank then click option'
ATask
Basync void
Cvoid
DIEnumerator
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' which cannot be awaited.
Using 'IEnumerator' which is for coroutines, not async/await.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps scene names to their async load tasks if the scene name length is greater than 5.

Unity
var loadTasks = scenes
  .Where(scene => [2] > 5)
  .ToDictionary(scene => scene, scene => [1]);
Drag options to blanks, or click blank then click option'
ASceneManager.LoadSceneAsync(scene).completed
BLength
Dscene.Length
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property name for string length.
Not calling the async load method properly.
5fill in blank
hard

Fill all three blanks to create a method that asynchronously loads a scene and logs when done.

Unity
public async Task [1](string sceneName) {
    await [2](sceneName).completed;
    Debug.Log([3] + " loaded");
}
Drag options to blanks, or click blank then click option'
ALoadSceneAsync
BSceneManager.LoadSceneAsync
CsceneName
DLoadAsyncScene
Attempts:
3 left
💡 Hint
Common Mistakes
Not awaiting the async load method.
Logging a wrong variable or string literal.