0
0
Unityframework~20 mins

Loading screens with coroutines in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loading Screen 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 coroutine loading simulation?

Consider this Unity coroutine that simulates a loading screen progress. What will be printed in the console?

Unity
using System.Collections;
using UnityEngine;

public class Loader : MonoBehaviour
{
    IEnumerator StartLoading()
    {
        int progress = 0;
        while (progress < 100)
        {
            progress += 20;
            Debug.Log($"Loading: {progress}%");
            yield return new WaitForSeconds(0.5f);
        }
        Debug.Log("Loading Complete!");
    }

    void Start()
    {
        StartCoroutine(StartLoading());
    }
}
A
Loading: 20%
Loading: 40%
Loading: 60%
Loading: 80%
Loading: 100%
Loading Complete!
B
Loading: 20%
Loading: 40%
Loading: 60%
Loading: 80%
Loading: 100%
C
Loading: 0%
Loading: 20%
Loading: 40%
Loading: 60%
Loading: 80%
Loading: 100%
Loading Complete!
D
Loading: 20%
Loading: 40%
Loading: 60%
Loading: 80%
Loading Complete!
Attempts:
2 left
💡 Hint

Check how the progress variable changes inside the loop and when the final message is printed.

🧠 Conceptual
intermediate
1:30remaining
Which statement about Unity coroutines and loading screens is true?

Choose the correct statement about using coroutines for loading screens in Unity.

ACoroutines allow asynchronous operations without freezing the main thread.
BCoroutines run on a separate thread and block the main thread until complete.
CCoroutines automatically display a loading screen UI without extra code.
DCoroutines cannot be stopped once started.
Attempts:
2 left
💡 Hint

Think about how coroutines help keep the game responsive during loading.

🔧 Debug
advanced
2:30remaining
Why does this loading coroutine never finish?

Examine the coroutine below. It is supposed to simulate loading progress but never prints "Loading Complete!". What is the cause?

Unity
IEnumerator LoadScene()
{
    float progress = 0f;
    while (progress <= 1f)
    {
        progress += 0.2f;
        Debug.Log($"Progress: {progress * 100}%");
        yield return new WaitForSeconds(0.5f);
    }
    Debug.Log("Loading Complete!");
}
AThe coroutine is missing a yield return null statement inside the loop.
BThe loop condition should be progress < 1f instead of <= 1f to avoid infinite loop.
CThe progress variable is a float and adding 0.2f causes floating point precision errors preventing loop exit.
DThe Debug.Log statement inside the loop causes the coroutine to freeze.
Attempts:
2 left
💡 Hint

Consider how floating point numbers behave when incremented repeatedly.

📝 Syntax
advanced
1:30remaining
Which option correctly defines a coroutine for a loading screen?

Identify the correct syntax for a Unity coroutine that waits 1 second and then prints "Done Loading".

A
IEnumerator Load() {
    yield return WaitForSeconds(1);
    Debug.Log("Done Loading");
}
B
void Load() {
    yield return new WaitForSeconds(1);
    Debug.Log("Done Loading");
}
C
IEnumerator Load() {
    yield WaitForSeconds(1);
    Debug.Log("Done Loading");
}
D
IEnumerator Load() {
    yield return new WaitForSeconds(1);
    Debug.Log("Done Loading");
}
Attempts:
2 left
💡 Hint

Remember the correct way to yield a WaitForSeconds in a coroutine.

🚀 Application
expert
3:00remaining
How to update a UI loading bar smoothly with a coroutine?

You want to update a UI loading bar fill amount smoothly from 0 to 1 over 3 seconds using a coroutine. Which code snippet achieves this?

Unity
using UnityEngine;
using UnityEngine.UI;

public class LoadingBar : MonoBehaviour
{
    public Image loadingBar;

    IEnumerator FillBar()
    {
        float elapsed = 0f;
        while (elapsed < 3f)
        {
            loadingBar.fillAmount = elapsed / 3f;
            elapsed += Time.deltaTime;
            yield return null;
        }
        loadingBar.fillAmount = 1f;
    }
}
A
IEnumerator FillBar() {
    float elapsed = 0f;
    while (elapsed &lt; 3f) {
        loadingBar.fillAmount = elapsed / 3f;
        elapsed += Time.deltaTime;
        yield return new WaitForSeconds(0.1f);
    }
    loadingBar.fillAmount = 1f;
}
B
IEnumerator FillBar() {
    float elapsed = 0f;
    while (elapsed &lt;= 3f) {
        loadingBar.fillAmount = elapsed / 3f;
        elapsed += Time.deltaTime;
        yield return null;
    }
    loadingBar.fillAmount = 1f;
}
C
IEnumerator FillBar() {
    float elapsed = 0f;
    while (elapsed &lt; 3f) {
        loadingBar.fillAmount = elapsed * 3f;
        elapsed += Time.deltaTime;
        yield return null;
    }
    loadingBar.fillAmount = 1f;
}
D
IEnumerator FillBar() {
    float elapsed = 0f;
    while (elapsed &lt; 3f) {
        loadingBar.fillAmount = 1f - (elapsed / 3f);
        elapsed += Time.deltaTime;
        yield return null;
    }
    loadingBar.fillAmount = 1f;
}
Attempts:
2 left
💡 Hint

Think about the condition to stop the loop and how to calculate fill amount as a fraction of elapsed time.