Bird
Raised Fist0
Unityframework~5 mins

Loading screens with coroutines in Unity

Choose your learning style10 modes available
Introduction

Loading screens help users know the game is working while content loads. Coroutines let us pause and wait without freezing the game.

When loading a new game level that takes time
When downloading data or assets in the background
When you want to show progress or animation during loading
When you want to keep the game responsive while waiting
When you want to delay actions without blocking the main game
Syntax
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.

Examples
This coroutine waits 2 seconds before continuing.
Unity
IEnumerator SimpleWait() {
    Debug.Log("Start waiting");
    yield return new WaitForSeconds(2f);
    Debug.Log("Waited 2 seconds");
}
This coroutine loads a scene and logs progress until done.
Unity
IEnumerator LoadSceneAsync() {
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Level1");
    while (!asyncLoad.isDone) {
        Debug.Log($"Loading progress: {asyncLoad.progress * 100}%");
        yield return null;
    }
    Debug.Log("Loading complete");
}
Sample Program

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.

Unity
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);
    }
}
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What is the main purpose of using coroutines for loading screens in Unity?
easy
A. To run loading tasks without freezing the game UI
B. To speed up the loading process by using multiple threads
C. To automatically optimize game assets during loading
D. To replace the Update() method for all game logic

Solution

  1. Step 1: Understand coroutine behavior

    Coroutines allow tasks to run over multiple frames without blocking the main thread.
  2. Step 2: Relate to loading screens

    Using coroutines for loading lets the UI remain responsive and update progress smoothly.
  3. Final Answer:

    To run loading tasks without freezing the game UI -> Option A
  4. Quick Check:

    Coroutines keep UI responsive during loading = A [OK]
Hint: Coroutines prevent freezing by spreading work over frames [OK]
Common Mistakes:
  • Thinking coroutines run on separate threads
  • Believing coroutines speed up loading automatically
  • Confusing coroutines with Update() method
2. Which of the following is the correct way to start a coroutine named LoadSceneAsync in Unity?
easy
A. StartCoroutine(LoadSceneAsync());
B. StartCoroutine(LoadSceneAsync);
C. StartCoroutine("LoadSceneAsync");
D. StartCoroutine.LoadSceneAsync();

Solution

  1. Step 1: Recall coroutine syntax

    Coroutines are started by calling StartCoroutine with the IEnumerator method invoked, like StartCoroutine(MethodName()).
  2. Step 2: Check options

    StartCoroutine(LoadSceneAsync()); correctly calls StartCoroutine with LoadSceneAsync() including parentheses.
  3. Final Answer:

    StartCoroutine(LoadSceneAsync()); -> Option A
  4. Quick Check:

    StartCoroutine needs method call with parentheses = A [OK]
Hint: StartCoroutine requires parentheses to call the IEnumerator method [OK]
Common Mistakes:
  • Omitting parentheses when calling the coroutine method
  • Using string names instead of method calls
  • Trying to call coroutine like a normal method
3. Given this coroutine code snippet, what will be printed to the console?
IEnumerator LoadScene()
{
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("GameScene");
    while (!asyncLoad.isDone)
    {
        Debug.Log($"Loading progress: {asyncLoad.progress * 100}%");
        yield return null;
    }
    Debug.Log("Loading complete!");
}
medium
A. Loading progress: 0% ... Loading progress: 100% ... Loading complete!
B. Loading progress: 0% ... Loading progress: 50% ... Loading complete!
C. Loading progress: 0% ... Loading progress: 90% ... Loading complete!
D. Only "Loading complete!" is printed

Solution

  1. Step 1: Understand AsyncOperation progress

    AsyncOperation.progress goes from 0 to 0.9 during loading; it reaches 0.9 before isDone becomes true.
  2. Step 2: Analyze the loop output

    The loop prints progress from 0% up to 90%, then exits when isDone is true, printing "Loading complete!".
  3. Final Answer:

    Loading progress: 0% ... Loading progress: 90% ... Loading complete! -> Option C
  4. Quick Check:

    AsyncOperation.progress maxes at 0.9 before done = C [OK]
Hint: AsyncOperation.progress maxes at 0.9 before completion [OK]
Common Mistakes:
  • Assuming progress reaches 100% before isDone
  • Expecting progress to jump directly to 100%
  • Ignoring that isDone triggers after 90% progress
4. Identify the error in this coroutine code for a loading screen:
IEnumerator LoadLevel()
{
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Level1");
    while (asyncLoad.isDone == false)
    {
        progressBar.fillAmount = asyncLoad.progress;
        yield return new WaitForSeconds(1);
    }
}
medium
A. AsyncOperation.progress should be multiplied by 100
B. The while loop condition should be asyncLoad.isDone == true
C. The coroutine never yields control to the main thread
D. Using WaitForSeconds delays UI updates unnecessarily

Solution

  1. Step 1: Check yield statement impact

    Using yield return new WaitForSeconds(1) pauses the coroutine for 1 second each loop, causing slow UI updates.
  2. Step 2: Suggest better yield usage

    Yielding null (yield return null) updates every frame, making progress bar smoother and more responsive.
  3. Final Answer:

    Using WaitForSeconds delays UI updates unnecessarily -> Option D
  4. Quick Check:

    WaitForSeconds slows progress updates = B [OK]
Hint: Use yield return null for smooth frame-by-frame updates [OK]
Common Mistakes:
  • Confusing progress value scale
  • Changing loop condition incorrectly
  • Not yielding at all causing freeze
5. You want to create a loading screen that shows a progress bar and a "Loading..." text that updates smoothly while loading a scene asynchronously. Which approach correctly combines coroutines and UI updates?
hard
A. Load the scene synchronously and update UI after loading finishes
B. Start a coroutine that updates progressBar.fillAmount with asyncLoad.progress every frame and updates loadingText with percentage until asyncLoad.isDone is true
C. Start a coroutine that waits 5 seconds then sets progressBar.fillAmount to 1 and loadingText to "Done"
D. Use Update() method to check asyncLoad.progress and update UI, no coroutine needed

Solution

  1. 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.
  2. 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.
  3. 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 B
  4. Quick Check:

    Coroutine + frame updates = smooth loading UI = D [OK]
Hint: Update UI inside coroutine each frame until loading completes [OK]
Common Mistakes:
  • Using synchronous loading blocking UI
  • Not updating UI every frame
  • Waiting fixed time instead of checking progress
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard