Skip to main content
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 ...
tyjkenn's user avatar
  • 2,586
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"...
Philipp's user avatar
  • 122k
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# ...
Peter's user avatar
  • 9,955
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 ...
Kevin's user avatar
  • 6,854
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 ...
David Weatherhead's user avatar
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 ...
DMGregory's user avatar
  • 136k
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 ...
Lasse's user avatar
  • 3,241
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 ...
Philipp's user avatar
  • 122k
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 ...
DMGregory's user avatar
  • 136k
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 ...
Philipp's user avatar
  • 122k
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 ...
Draco18s no longer trusts SE's user avatar
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: ...
Ed Marty's user avatar
  • 5,249
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'...
Bluff's user avatar
  • 31
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 ...
Galandil's user avatar
  • 1,284
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: ...
DMGregory's user avatar
  • 136k
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 ...
PepeOjeda's user avatar
  • 866
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 ...
Michael's user avatar
  • 51
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 ...
DMGregory's user avatar
  • 136k
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 ...
tomsseisums's user avatar
  • 3,376
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: ...
VoodaGod's user avatar
2 votes

Coroutine not working (Unity)

You need to edit your code something like this: ...
Muhammad Faizan Khan's user avatar
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 ...
ratchet freak's user avatar
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 ...
LifGwaethrakindo's user avatar
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 ...
Imperial Justinian's user avatar
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 <...
Philipp's user avatar
  • 122k
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: ...
DMGregory's user avatar
  • 136k
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 ...
Zebraman's user avatar
  • 819
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: ...
user3797758's user avatar
  • 3,641
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 ...
Sainath Krishnan's user avatar
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 ...
Hesamom's user avatar
  • 21

Only top scored, non community-wiki answers of a minimum length are eligible