Loading screens help users know the game is working while content loads. Coroutines let us pause and wait without freezing the game.
Loading screens with coroutines in Unity
IEnumerator LoadSceneWithLoadingScreen() {
// Show loading screen
loadingScreen.SetActive(true);
// Start loading scene asynchronously
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("SceneName");
// Wait until loading is done
while (!asyncLoad.isDone) {
// Optionally update progress bar here
yield return null;
}
// Hide loading screen
loadingScreen.SetActive(false);
}Coroutines use IEnumerator and yield return to pause execution.
Use AsyncOperation to load scenes without freezing the game.
IEnumerator SimpleWait() {
Debug.Log("Start waiting");
yield return new WaitForSeconds(2f);
Debug.Log("Waited 2 seconds");
}IEnumerator LoadSceneAsync() {
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Level1");
while (!asyncLoad.isDone) {
Debug.Log($"Loading progress: {asyncLoad.progress * 100}%");
yield return null;
}
Debug.Log("Loading complete");
}This Unity script shows a loading screen with a progress bar while loading a scene called "SampleScene". It uses a coroutine to update the progress bar smoothly and only hides the loading screen after the scene is ready.
using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class LoadingScreenController : MonoBehaviour { public GameObject loadingScreen; public Slider progressBar; public void StartLoading() { StartCoroutine(LoadSceneWithLoadingScreen()); } IEnumerator LoadSceneWithLoadingScreen() { loadingScreen.SetActive(true); AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("SampleScene"); asyncLoad.allowSceneActivation = false; while (!asyncLoad.isDone) { float progress = Mathf.Clamp01(asyncLoad.progress / 0.9f); progressBar.value = progress; if (asyncLoad.progress >= 0.9f) { progressBar.value = 1f; asyncLoad.allowSceneActivation = true; } yield return null; } loadingScreen.SetActive(false); } }
Always activate and deactivate the loading screen GameObject to show or hide it.
Use asyncLoad.allowSceneActivation = false to control when the scene switches.
Update UI elements like progress bars inside the coroutine for smooth feedback.
Loading screens keep users informed during long waits.
Coroutines let you wait without freezing the game.
Use AsyncOperation with coroutines to load scenes smoothly.
Practice
Solution
Step 1: Understand coroutine behavior
Coroutines allow tasks to run over multiple frames without blocking the main thread.Step 2: Relate to loading screens
Using coroutines for loading lets the UI remain responsive and update progress smoothly.Final Answer:
To run loading tasks without freezing the game UI -> Option AQuick Check:
Coroutines keep UI responsive during loading = A [OK]
- Thinking coroutines run on separate threads
- Believing coroutines speed up loading automatically
- Confusing coroutines with Update() method
LoadSceneAsync in Unity?Solution
Step 1: Recall coroutine syntax
Coroutines are started by calling StartCoroutine with the IEnumerator method invoked, like StartCoroutine(MethodName()).Step 2: Check options
StartCoroutine(LoadSceneAsync()); correctly calls StartCoroutine with LoadSceneAsync() including parentheses.Final Answer:
StartCoroutine(LoadSceneAsync()); -> Option AQuick Check:
StartCoroutine needs method call with parentheses = A [OK]
- Omitting parentheses when calling the coroutine method
- Using string names instead of method calls
- Trying to call coroutine like a normal method
IEnumerator LoadScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("GameScene");
while (!asyncLoad.isDone)
{
Debug.Log($"Loading progress: {asyncLoad.progress * 100}%");
yield return null;
}
Debug.Log("Loading complete!");
}Solution
Step 1: Understand AsyncOperation progress
AsyncOperation.progress goes from 0 to 0.9 during loading; it reaches 0.9 before isDone becomes true.Step 2: Analyze the loop output
The loop prints progress from 0% up to 90%, then exits when isDone is true, printing "Loading complete!".Final Answer:
Loading progress: 0% ... Loading progress: 90% ... Loading complete! -> Option CQuick Check:
AsyncOperation.progress maxes at 0.9 before done = C [OK]
- Assuming progress reaches 100% before isDone
- Expecting progress to jump directly to 100%
- Ignoring that isDone triggers after 90% progress
IEnumerator LoadLevel()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Level1");
while (asyncLoad.isDone == false)
{
progressBar.fillAmount = asyncLoad.progress;
yield return new WaitForSeconds(1);
}
}Solution
Step 1: Check yield statement impact
Using yield return new WaitForSeconds(1) pauses the coroutine for 1 second each loop, causing slow UI updates.Step 2: Suggest better yield usage
Yielding null (yield return null) updates every frame, making progress bar smoother and more responsive.Final Answer:
Using WaitForSeconds delays UI updates unnecessarily -> Option DQuick Check:
WaitForSeconds slows progress updates = B [OK]
- Confusing progress value scale
- Changing loop condition incorrectly
- Not yielding at all causing freeze
Solution
Step 1: Understand asynchronous loading with UI updates
Using a coroutine to check asyncLoad.progress each frame allows smooth progress bar and text updates during loading.Step 2: Evaluate options for UI responsiveness
Start a coroutine that updates progressBar.fillAmount with asyncLoad.progress every frame and updates loadingText with percentage until asyncLoad.isDone is true correctly updates UI every frame until loading completes; other options either block UI or do not update smoothly.Final Answer:
Start a coroutine that updates progressBar.fillAmount with asyncLoad.progress every frame and updates loadingText with percentage until asyncLoad.isDone is true -> Option BQuick Check:
Coroutine + frame updates = smooth loading UI = D [OK]
- Using synchronous loading blocking UI
- Not updating UI every frame
- Waiting fixed time instead of checking progress
Ready to prove it?
Pass the test and this pattern moves unknown
