📖 Scenario: Imagine you are making a simple game in Unity where you want to show a message for a few seconds and then hide it automatically. You want to learn how to use coroutines to handle this time-based action smoothly without freezing the game.
🎯 Goal: Build a Unity script that uses a coroutine to display a message for 3 seconds and then hide it, demonstrating how coroutines help manage time-based logic easily.
📋 What You'll Learn
Create a GameObject variable called messageObject to represent the message.
Create a float variable called displayTime and set it to 3.
Write a coroutine method called ShowMessage that activates the message, waits for displayTime seconds, then deactivates the message.
Start the coroutine in the Start method.
Print "Message shown and hidden after 3 seconds" after the coroutine finishes.
💡 Why This Matters
🌍 Real World
Games often need to show messages, animations, or effects for a set time without stopping the whole game. Coroutines let you do this easily.
💼 Career
Understanding coroutines is essential for Unity developers to manage timed events, animations, and smooth gameplay experiences.
Progress0 / 4 steps
1
Set up the message GameObject
Create a public GameObject variable called messageObject inside a class named MessageController.
Unity
Hint
Remember to declare messageObject as public so you can assign it in the Unity Editor.
2
Add the display time variable
Add a public float variable called displayTime and set it to 3f inside the MessageController class.
Unity
Hint
Use 3f to specify a float value of 3 seconds.
3
Write the coroutine to show and hide the message
Write a coroutine method called ShowMessage that activates messageObject, waits for displayTime seconds using yield return new WaitForSeconds(displayTime), then deactivates messageObject.
Unity
Hint
Coroutines use IEnumerator and yield return to pause execution without freezing the game.
4
Start the coroutine and print the completion message
In the Start method, start the ShowMessage coroutine using StartCoroutine(ShowMessage()). After the coroutine finishes, print "Message shown and hidden after 3 seconds".
Unity
Hint
The Start method runs when the game starts. Use it to begin your coroutine.
Practice
(1/5)
1. Why are coroutines useful for handling time-based logic in Unity?
easy
A. Because they run code faster than normal functions
B. Because they allow pausing code execution without freezing the game
C. Because they automatically optimize memory usage
D. Because they replace the Update method completely
Solution
Step 1: Understand coroutine behavior
Coroutines can pause execution at certain points and resume later without stopping the whole game.
Step 2: Compare with other methods
Normal functions run all at once, freezing the game if delayed. Coroutines avoid this by yielding control.
Final Answer:
Because they allow pausing code execution without freezing the game -> Option B
Quick Check:
Pause without freeze = A [OK]
Hint: Coroutines pause without freezing game flow [OK]
Common Mistakes:
Thinking coroutines run code faster
Believing coroutines replace Update method
Assuming coroutines optimize memory automatically
2. Which of the following is the correct way to start a coroutine in Unity?
easy
A. StartCoroutine(MyCoroutine());
B. RunCoroutine(MyCoroutine);
C. BeginCoroutine(MyCoroutine());
D. InvokeCoroutine(MyCoroutine);
Solution
Step 1: Recall Unity coroutine syntax
Unity uses StartCoroutine with parentheses to start a coroutine method.
Step 2: Check each option
Only StartCoroutine(MyCoroutine()); matches Unity's API exactly.
Final Answer:
StartCoroutine(MyCoroutine()); -> Option A
Quick Check:
StartCoroutine with parentheses = A [OK]
Hint: Use StartCoroutine with parentheses to run coroutine [OK]
Common Mistakes:
Using wrong method names like RunCoroutine
Omitting parentheses when calling coroutine
Confusing Invoke with coroutine start
3. What will be the output of this coroutine code snippet?
IEnumerator PrintNumbers()
{
for (int i = 1; i <= 3; i++)
{
Debug.Log(i);
yield return new WaitForSeconds(1);
}
}
StartCoroutine(PrintNumbers());
medium
A. Prints 1, then waits 1 second, prints 2, waits 1 second, then prints 3
B. Prints 3 three times with 1 second delay
C. Prints 1, 2, 3 immediately without delay
D. Does not print anything because coroutine is not started
Solution
Step 1: Analyze the loop and yield
The loop prints i, then waits 1 second before continuing to next iteration.
Step 2: Understand coroutine timing
Each Debug.Log happens with a 1 second pause between prints, so output is 1, wait, 2, wait, 3.
Final Answer:
Prints 1, then waits 1 second, prints 2, waits 1 second, then prints 3 -> Option A
Quick Check:
Yield WaitForSeconds causes delay between prints = C [OK]
WaitForSeconds must be preceded by yield to pause execution properly.
Step 2: Verify method signature and usage
Coroutine returns IEnumerator and StartCoroutine accepts it, Debug.Log works inside coroutines.
Final Answer:
Missing yield before WaitForSeconds -> Option D
Quick Check:
Always yield WaitForSeconds in coroutine = B [OK]
Hint: Always use 'yield' before WaitForSeconds in coroutines [OK]
Common Mistakes:
Omitting yield keyword
Changing return type to void
Thinking Debug.Log is disallowed in coroutines
5. You want to create a blinking light effect that turns on and off every 0.5 seconds using a coroutine. Which approach correctly handles the timing without freezing the game?
To wait 0.5 seconds between toggles without freezing, yield return WaitForSeconds(0.5f) is needed.
Step 2: Evaluate other options
Thread.Sleep blocks the main thread (freezes game), yield return null waits only one frame, InvokeRepeating is a different method, not inside coroutine.
Final Answer:
yield return new WaitForSeconds(0.5f); -> Option C
Quick Check:
Use yield WaitForSeconds for timed pause in coroutine = D [OK]
Hint: Use yield WaitForSeconds for smooth timed waits [OK]
Common Mistakes:
Using Thread.Sleep which freezes game
Using yield return null which waits only one frame
Confusing InvokeRepeating with coroutine yield
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.