50
votes
Accepted
What is wrong with my Respawn coroutine?
This doesn't seem to have anything to do with how you set up the coroutine. What is probably happening is that the player gets a high enough acceleration while falling to glitch past the ground and ...
24
votes
Accepted
Are there limits to the number of coroutines in Unity?
I never heard of anything like that. The documentation on coroutines does not mention any of that either. And it also sounds like one of those "If you have to ask, you are doing things wrong"...
15
votes
Accepted
Why am I not getting stuck in the loop
The reason is the keyword yield which has a specific meaning in C#.
On encountering the words yield return a function in C# ...
10
votes
Should I use Async & Await, instead of Coroutines, in Unity?
In contrast to Evorlor's answer, here are some good reasons to use Coroutines:
They've been the standard solution in Unity for many years and anyone else you bring onto the team should already be ...
8
votes
Why am I not getting stuck in the loop
When the fire button is lifted, the second if statement is entered, and StopAllCoroutines is run. This means that the Coroutine that the while loop is running in is ended, thus there is no more ...
6
votes
Accepted
NullReferenceException in StartCoroutine method
A bit of background on this:
StartCoroutine is a method provided by the MonoBehaviour base class.
MonoBehaviours are designed to be attached as components to GameObjects - Unity will warn you if you ...
4
votes
Accepted
How do I use coroutines to animate properly?
Calling yield break breaks the coroutine and the while loop in it won't start over again. The while loop goes through only once after waiting for 0.3 seconds. Use ...
4
votes
Accepted
How can I optimize coroutines?
The obvious solution would of course be to not yield after every queue entry but only after every n entries, where n is a number ...
4
votes
Accepted
Coroutine delay isn't consistent on different framerates
The smallest delay that WaitForSeconds can give you is one frame. That's because the way it's implemented is it checks once each frame (after ...
3
votes
Accepted
How can I use a coroutine and GetAxis to rotate a game object?
If each call to Update calls a coroutine, then you are creating a new coroutine each update and each coroutine will run in parallel with those created before. Your ...
3
votes
Why even use coroutines in Unity?
You can do that if you want, but...
First, 10000 update calls are more expensive than one update call calling 10000 functions. This is due to the Magic Method indexing and querying logic Unity does ...
3
votes
Why am I not getting stuck in the loop
Coroutines are a strange beast. Yield return causes the method to suspend execution until it is later stepped. Behind the scenes, it might look something like this:
...
3
votes
Possible to yield a coroutine when it is taking too long to run?
This is my version of Leo's answer. Pretty handy tool for splitting up the workload of certain loops to help with FPS. Bit hard to predict how long each loop will take though, but probably something I'...
3
votes
Accepted
Return value from coroutine to non monobehaviour
It's not true that you can't make a method in a base class working as a coroutine.
If I understood you correctly, the AssessGeneration method is inside your base ...
3
votes
Accepted
How does a "static" IEnumerator work in this context? (The code works, but what is it actually doing?)
You can build a quick synthetic test to find out:
...
3
votes
Accepted
Can player input be missed while yielding WaitForSeconds?
Yes, that can definitely cause you to miss the input. Since you are manually checking wasPressedThisFrame, the only way to guarantee that you won't miss it is to ...
3
votes
Accepted
Audio won't pause in coroutine
Okay, I figured out the issue. It is pausing the audio, but since I left soundFX.UnPause(); inside the if (!pausing) block ...
3
votes
How to make a coroutine private in Unity?
Where you put your code matters.
When you write code like this:
private void UpdateTarget() {
// Update code here...
}
All the code between the opening ...
2
votes
Accepted
GameObject that should have a fixed speed keeps accelerating
Turned my comment into answer, to make the question answered.
The problem lies in the while loop never actually reaching it's condition.
Only the position is ...
2
votes
Accepted
How to await player input using Coroutines
try this, you simply give the WaitForKeyDown coroutine a list of keys to check, once it registers a key, it will exit the checking loop & terminate:
...
2
votes
2
votes
Unity: How to resize a game object for a certain amount of time?
GameObject originPlayer = new GameObject();
originPlayer = player;
this doesn't do what you think it does.
instead you want to save the just the original scale ...
2
votes
Accepted
Unity: Loading screen without a coroutine (is it possible?)
The reason why the button gets stuck until the scene loads is, you are not loading the mesh in a parallel thread or a coroutine that allows the game loop to keep ticking, hence, you are loading it in ...
2
votes
How can I make a coroutine run for a specific number of times in Unity?
Unity Coroutines will exit automatically when they reach the end of the function. Thus, the answer is simply to use a for loop instead of a while loop.
If you need the number of times it runs for to ...
2
votes
Accepted
Help understanding IEnumerator Move routine
I assume the lines you don't understand are those you put comments on, so I will explain them in order.
A method returning an IEnumerator is one which can use the <...
2
votes
Accepted
How to create loading bar for asset bundles on Unity (without changing scene)?
Remember, when you start a coroutine, your execution doesn't block and wait for it to finish. It continues to the next line immediately.
So, the problems in your code are:
...
2
votes
Accepted
In Unity, how do I use the extra time in every frame to do some extra processing?
This (general) approach is used in several AAA engines, but isn't very compatible with the way current Unity (2017) works.
To do what you've described, you'd have to build your own task system that ...
2
votes
Accepted
Recursive Unity Coroutine
Though the comments on this question I was able to find a solution to this. If I transfer it over to the example I posted above it would look like this:
...
2
votes
Coroutine only partially executing
I have fixed this by turning the function I use to initialize the Coroutine into an IEnumerator, and adding a yield return to the call.
(The same effect can be ...
2
votes
Why coroutine gets stopped?
Coroutines are stopped when their corresponding game object is deactivated or destroyed, regarding your code logic, it's possible after the scene change, the game object in previous scene as well as ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
coroutines × 98unity × 92
c# × 42
multithreading × 9
2d × 3
frame-rate × 3
monobehaviour × 3
animation × 2
architecture × 2
optimization × 2
collider × 2
android × 1
rotation × 1
networking × 1
ai × 1
procedural-generation × 1
vector × 1
gui × 1
input × 1
transformation × 1
game-loop × 1
raycasting × 1
assets × 1
unityscript × 1
godot × 1