Consider this Unity C# script snippet using async/await. What will be printed in the Unity console?
using System.Threading.Tasks; using UnityEngine; public class AsyncTest : MonoBehaviour { async void Start() { Debug.Log("Start"); await Task.Delay(1000); Debug.Log("After delay"); } }
Think about how async/await works with Task.Delay in Unity's main thread.
The Start method prints 'Start' immediately, then awaits Task.Delay(1000) which pauses asynchronously for 1 second without blocking the main thread. After the delay, it prints 'After delay'.
Choose the correct statement about using async/await in Unity.
Consider the difference between async void and async Task in C#.
async Task methods return a Task and can be awaited, allowing exceptions to be caught. async void methods cannot be awaited and are only recommended for event handlers. Unity's main thread can run async methods without blocking.
Examine this code snippet. Why does it cause the Unity editor to freeze?
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"; } }
Think about what happens when you block the main thread waiting for an async method that also needs the main thread.
Calling .Result blocks the main thread waiting for GetData to complete. But GetData awaits Task.Delay and tries to resume on the main thread, which is blocked. This causes a deadlock freezing Unity.
Choose the correct syntax for an async method that returns an integer after a delay.
Remember the correct order of async and return type for methods returning a value.
The correct syntax is 'async Task
Analyze this Unity C# code. How many times will 'Loading...' be printed in the console?
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"); } }
Consider how the for loop and await work together in async methods.
The loop runs 3 times. Each iteration prints 'Loading...', then awaits 500ms before continuing. So 'Loading...' prints exactly 3 times, then 'Done' prints once.