Challenge 2: Coroutine delay
- Game Development course
- Intermediate
- Lesson 80/83
- 5 min
Write a coroutine that prints a delayed message with WaitForSeconds.
Using Coroutines and WaitForSeconds
In Unity, Coroutines are used to pause execution for a given time before performing an action. Inside a method returning IEnumerator, yield return new WaitForSeconds(seconds); suspends execution and resumes once the duration elapses. To run a coroutine, call StartCoroutine().
IEnumerator DelayedMessage()
{
yield return new WaitForSeconds(2.0f);
Debug.Log("Time elapsed!");
}Practice Inspect Delay Duration
Instantiate WaitForSeconds bekleme = new WaitForSeconds(saniye); and print it with Debug.Log("Beklendi: " + bekleme.seconds);.
Starter code
using System;
public class WaitForSeconds
{
public float seconds;
public WaitForSeconds(float sec) { this.seconds = sec; }
}
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
float saniye = 2.0f;
// WaitForSeconds olustur ve suresini konsola yazdir
}
}Sample solution
using System;
public class WaitForSeconds
{
public float seconds;
public WaitForSeconds(float sec) { this.seconds = sec; }
}
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
float saniye = 2.0f;
WaitForSeconds bekleme = new WaitForSeconds(saniye);
Debug.Log("Beklendi: " + bekleme.seconds);
}
}Expected output
Beklendi: 2
Practice Step Flow Simulation
Print Debug.Log("Basladi"); followed by Debug.Log("Bitti"); in order.
Starter code
using System;
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
// Basladi ve Bitti mesajlarini sirayla yazdir
}
}Sample solution
using System;
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
Debug.Log("Basladi");
Debug.Log("Bitti");
}
}Expected output
Basladi Bitti
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which yield statement in Unity causes code execution within a coroutine to pause for a specified number of seconds?
- yield return null;
- yield break;
- yield return new WaitUntil();
- yield return new WaitForSeconds(saniye); Correct answer
Explanation: When a WaitForSeconds object is returned with yield return, execution pauses for the specified seconds.
2 Start Coroutine
StartCoroutine(____());
StartCoroutine(GecikmeliGorev());
Explanation: The IEnumerator function call to execute is passed as a parameter to the StartCoroutine method.
3 Coroutine Concepts
| Term | Meaning |
|---|---|
IEnumerator |
Mandatory coroutine return type |
WaitForSeconds |
Yield waiting for specified seconds |
StartCoroutine |
Call starting coroutine execution |
yield return |
Keyword pausing coroutine execution |