📖 Scenario: You are making a simple Unity game where an object moves up and down repeatedly using a coroutine. You want to learn how to stop this movement when the player presses a key.
🎯 Goal: Build a Unity script that starts a coroutine to move an object up and down, then stops the coroutine when the player presses the spacebar.
📋 What You'll Learn
Create a coroutine that moves an object up and down repeatedly.
Store the coroutine in a variable to control it.
Stop the coroutine when the spacebar is pressed.
Print a message when the coroutine stops.
💡 Why This Matters
🌍 Real World
Coroutines are used in games to run tasks over time without freezing the game, like animations or timed events. Knowing how to stop them lets you control game behavior smoothly.
💼 Career
Game developers often use coroutines in Unity to manage animations, delays, and sequences. Being able to start and stop coroutines is essential for responsive and efficient game code.
Progress0 / 4 steps
1
Create the MoveUpDown coroutine
Create a coroutine method called MoveUpDown that moves the object up by 1 unit, waits for 1 second, moves it down by 1 unit, and waits for 1 second in a loop.
Unity
Hint
Use IEnumerator to create the coroutine method. Use a while (true) loop to repeat the movement.
2
Start the coroutine and save it
In the Start method, start the MoveUpDown coroutine and save it in a variable called moveCoroutine of type Coroutine.
Unity
Hint
Use StartCoroutine and assign it to moveCoroutine to keep a reference.
3
Stop the coroutine on spacebar press
Add an Update method that checks if the spacebar is pressed using Input.GetKeyDown(KeyCode.Space). If pressed, stop the moveCoroutine using StopCoroutine(moveCoroutine) and print "Coroutine stopped".
Unity
Hint
Use Input.GetKeyDown(KeyCode.Space) to detect the spacebar press and StopCoroutine to stop the running coroutine.
4
Print confirmation when coroutine stops
Make sure the program prints "Coroutine stopped" to the console when the coroutine is stopped by pressing the spacebar.
Unity
Hint
Press the spacebar while the game is running to see the message in the console.
Practice
(1/5)
1. What is the main purpose of stopping a coroutine in Unity?
easy
A. To halt an ongoing task or process before it finishes
B. To start a new coroutine automatically
C. To save the game state permanently
D. To increase the frame rate of the game
Solution
Step 1: Understand what coroutines do
Coroutines run tasks over time, like waiting or animations.
Step 2: Purpose of stopping coroutines
Stopping a coroutine means halting that ongoing task before it naturally ends.
Final Answer:
To halt an ongoing task or process before it finishes -> Option A
Quick Check:
Stopping coroutine = halting task [OK]
Hint: Stopping coroutines means stopping tasks in progress [OK]
Common Mistakes:
Confusing stopping with starting coroutines
Thinking stopping saves game data
Assuming stopping affects frame rate
2. Which of the following is the correct syntax to stop a coroutine by its reference in Unity C#?
easy
A. EndCoroutine(coroutineReference);
B. StopCoroutineStart(coroutineReference);
C. Stop(coroutineReference);
D. StopCoroutine(coroutineReference);
Solution
Step 1: Recall Unity's coroutine stopping method
Unity uses StopCoroutine() to stop coroutines by reference or name.
Step 2: Check method names
Only StopCoroutine() matches Unity's API; others are invalid.
Final Answer:
StopCoroutine(coroutineReference); -> Option D
Quick Check:
Correct method name = StopCoroutine [OK]
Hint: Use StopCoroutine() exactly to stop by reference [OK]
Common Mistakes:
Using incorrect method names like Stop() or EndCoroutine()
Adding extra words like Start in method name
Confusing with StartCoroutine syntax
3. What will be the output of the following Unity C# code snippet?
IEnumerator ExampleCoroutine() {
Debug.Log("Start");
yield return new WaitForSeconds(2);
Debug.Log("End");
}
void Start() {
Coroutine c = StartCoroutine(ExampleCoroutine());
StopCoroutine(c);
Debug.Log("Stopped");
}
medium
A. Start\nEnd\nStopped
B. Stopped
C. Start\nStopped
D. Start\nStopped\nEnd
Solution
Step 1: Analyze coroutine execution
Coroutine logs "Start", waits 2 seconds, then logs "End".
Step 2: Effect of StopCoroutine
StopCoroutine(c) stops it immediately after "Start" before "End" logs.
Final Answer:
Start\nStopped -> Option C
Quick Check:
StopCoroutine stops before "End" logs [OK]
Hint: StopCoroutine halts before delayed logs run [OK]
Common Mistakes:
Assuming 'End' logs after stopping
Ignoring immediate stop effect
Thinking 'Stopped' logs inside coroutine
4. Identify the error in this Unity C# code snippet that tries to stop a coroutine: