0
0
Unityframework~20 mins

Async/await in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async/Await Unity Master
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 async method in Unity?

Consider this Unity C# script snippet using async/await. What will be printed in the Unity console?

Unity
using System.Threading.Tasks;
using UnityEngine;

public class AsyncTest : MonoBehaviour
{
    async void Start()
    {
        Debug.Log("Start");
        await Task.Delay(1000);
        Debug.Log("After delay");
    }
}
AStart immediately, then after 1 second print 'After delay'
BOnly prints 'Start' and never prints 'After delay'
CThrows a runtime exception because Task.Delay is not supported
DPrints 'After delay' first, then 'Start'
Attempts:
2 left
💡 Hint

Think about how async/await works with Task.Delay in Unity's main thread.

🧠 Conceptual
intermediate
1:30remaining
Which statement about async/await in Unity is true?

Choose the correct statement about using async/await in Unity.

Aasync void methods are recommended for all Unity event functions to handle async code
Bawait Task.Delay blocks the main thread in Unity
Casync Task methods can be awaited and allow better error handling than async void
DUnity's main thread cannot run async methods at all
Attempts:
2 left
💡 Hint

Consider the difference between async void and async Task in C#.

🔧 Debug
advanced
2:30remaining
Why does this async method cause a deadlock in Unity?

Examine this code snippet. Why does it cause the Unity editor to freeze?

Unity
using System.Threading.Tasks;
using UnityEngine;

public class DeadlockExample : MonoBehaviour
{
    void Start()
    {
        var result = GetData().Result;
        Debug.Log(result);
    }

    async Task<string> GetData()
    {
        await Task.Delay(1000);
        return "Data loaded";
    }
}
ADebug.Log cannot be called inside Start method
BTask.Delay is not awaited properly causing infinite wait
CGetData method is missing async keyword
DCalling .Result on an async Task blocks the main thread causing a deadlock
Attempts:
2 left
💡 Hint

Think about what happens when you block the main thread waiting for an async method that also needs the main thread.

📝 Syntax
advanced
1:30remaining
Which option correctly defines an async method returning a value in Unity?

Choose the correct syntax for an async method that returns an integer after a delay.

Aasync int GetNumber() { await Task.Delay(500); return 42; }
Basync Task<int> GetNumber() { await Task.Delay(500); return 42; }
CTask<int> async GetNumber() { await Task.Delay(500); return 42; }
Dasync void GetNumber() { await Task.Delay(500); return 42; }
Attempts:
2 left
💡 Hint

Remember the correct order of async and return type for methods returning a value.

🚀 Application
expert
2:00remaining
How many times will 'Loading...' print in this Unity async loop?

Analyze this Unity C# code. How many times will 'Loading...' be printed in the console?

Unity
using System.Threading.Tasks;
using UnityEngine;

public class LoopAsync : MonoBehaviour
{
    async void Start()
    {
        for (int i = 0; i < 3; i++)
        {
            Debug.Log("Loading...");
            await Task.Delay(500);
        }
        Debug.Log("Done");
    }
}
A3 times, once per loop iteration
B1 time, because await delays the whole loop
C0 times, because Debug.Log is skipped in async methods
DInfinite times, because the loop never ends
Attempts:
2 left
💡 Hint

Consider how the for loop and await work together in async methods.