Questions tagged [coroutines]

Coroutines are software components that generalize subroutines for cooperative multitasking. They are suited for asynchronous (or non-blocking) programming and allow program execution to be suspended and resumed.

Filter by
Sorted by
Tagged with
0 votes
0 answers
52 views

Is there a way around using yield return null for coroutines to avoid waiting an extra frame at the end of my coroutine?

I've gotten used to using coroutines as a way to ensure a things are done sequentially in my code, i.e. instead of calling method a, method b, and method c on back to back lines I will sometimes yield ...
0 votes
1 answer
110 views

Deal damage just once, rather than repeatedly till the enemy dies

I am new to making games with Unity. I am trying to damage the enemy. So far, the damage works. The problem is it will repeat it until the enemy is dead, but I want it to damage the enemy only once. <...
0 votes
1 answer
77 views

Unity coroutine not yielding sometimes

I'm making a 2D platformer controller and I want to implement a coyote time and buffered jump, the way I implement both is by setting a bool to true when either action is enabled like this: ...
0 votes
0 answers
112 views

Unity Coroutine freezes game even though no loop

Every time I call this function that starts the coroutine Faint, it freezes the game. I do not understand why this freezes the game though, because the coroutine does not have any loop. ...
  • 137
0 votes
1 answer
73 views

How to change animation coroutine when a variable changes, not every frame?

I want to be able to change the enum animation mode at runtime. This way will start the coroutine every frame in Update, but I want to start the coroutine only if the value of the enum variable has ...
0 votes
2 answers
260 views

How does a "static" IEnumerator work in this context? (The code works, but what is it actually doing?)

I am generating many objects procedurally in my Unity C# app/game such as user interfaces, user profiles, etc. I have made classes which feature static variables/functions to allow this. Everything ...
  • 451
0 votes
2 answers
58 views

Why coroutine gets stopped?

Here's my code: ...
  • 671
0 votes
0 answers
36 views

Gun keeps firing before the player is finished rotating

I have a script that manages player rotation and shooting. When I click on an enemy, the player is supposed to turn toward the enemy and then shoot in the direction they are facing. However, the ...
1 vote
0 answers
95 views

"Assertion failed" error when running coroutines in edit mode - Unity

TLDR: Calling coroutines from OnValidate in edit mode leads to following error: ...
-1 votes
2 answers
63 views

Why can't I stop Coroutine?

...
  • 1
0 votes
0 answers
40 views

How to change fall delay of a gameobject by time?

I want my gameobjects to fall faster by time as the player collides with them. What I mean is, when the game starts, the gameobjects fall off after 1 second the player collided with them, but after 60 ...
0 votes
1 answer
110 views

Triggers VS coroutine performance?

So I am using unity and wondering if I have an agent going to move to a position say Agent A moves to position B in x seconds guaranteed (avoids all physics or things that could disrupt the path). ...
  • 139
1 vote
1 answer
213 views

Wait for a function to return value without impacting framerate

I'm looking to do some possibly heavy calculation in my game. An agent in my game needs to evaluate up to 1000 objects while performing heavy calculations to select his next job. I want to either ...
0 votes
1 answer
198 views

check if joystick magnitude is increasing

I use joystick mechanic. What I want to do is: Translating my object forward when my joystickInput.magnitude is increasing. Translating backward when my ...
0 votes
0 answers
7 views

How to temporarily set a game object active? [duplicate]

I'm trying to an use IEnumerator method called Wait() to temporarily enable and then wait, and disable a gameobject within a method. Basically I'm trying to create a muzzle flash within a shoot method,...
0 votes
1 answer
115 views

Using Lua to define NPC behaviour

I'm working on a RPG in C using Lua for event scripting like NPC behavior. I ran into a design problem. I have created Lua threads in the C API for each game object which can be a NPC. So this: ...
0 votes
2 answers
58 views

Unexpected Result On Coroutines Called From Another Script

So I performed tests on calling coroutines from another script. I want to understand why the results aren't the expected behavior in one of the test case, and unfortunately the one that I needed to ...
  • 21
0 votes
2 answers
195 views

How to fix issues with repeated coroutines for lerping values?

I have created a progress bar system that lerps the value over a duration to provide some life to the progress bars. I've done this using a coroutine that gets called each time a value (such as life ...
1 vote
1 answer
204 views

Design pattern for menu transitions in C# (Unity)

I am working on my first project in Unity using C# and I am currently trying to design a menu system that does the following sequence of events when a user clicks a button: Button does some effect ...
  • 1,507
0 votes
1 answer
638 views

Unity3D: Download multiple files and implement a callback

...
  • 311
10 votes
2 answers
10k views

Should I use Async & Await, instead of Coroutines, in Unity?

Should I use Async & Await in Unity? Or should I keep using Coroutines? If I should use Async & Await, how can I do so in a manner comparable to Coroutines?
  • 5,272
0 votes
3 answers
183 views

How can I stop and resume a coroutine over and over again?

I want a coroutine to enter by default and then to stop when any input is given to it but then after some code to return again as active. So basically to start and stop a coroutine as many times as it'...
  • 5
0 votes
1 answer
55 views

Unity: How to connect to server with status in dialog

I am trying to build in unity a basic logon screen. Now I'm building on something if the connection will have delay or so... There will be a dialog, where the progress of connection to server will be ...
  • 57
0 votes
0 answers
11 views

Coroutine problem, I don't know what's happening [duplicate]

Calling function. ...
0 votes
1 answer
194 views

How to call StartCourotine in abstract class?

Bellow is my code. I have an abstract class and a inheriting class. This keeps giving me an NullReferenceException, on the ...
  • 145
0 votes
0 answers
20 views

FixedUpdate or Subroutine

So Update is called every frame while FixedUpdate is called on Physics time, which can be modified. Is it better to depend on FixedUpdate to have optimized game loop so that everything doesn't get ...
0 votes
1 answer
59 views

Unity both duration and cooldown system

I have a boss with machine gun and i am trying to make him shoot for 5 seconds and then stop for 5 seconds and so on. I tried to create an IEnumerator and call it on Start (as far as i know i can not ...
  • 5
0 votes
1 answer
163 views

How to start a coroutine that will finish after the object is destroyed

I have a simple game in which the player can collect power-ups. Each power-up gives the player a temporary advantage for several seconds. The power-up should disappear immediately after the player ...
0 votes
1 answer
144 views

How to batch coroutines (IEnumerators) in parallel

We are working in a context where multithreading is not an option, so we're using coroutines to handle our loads. Often there are multiple things that need to be downloaded, leading to a pattern like ...
  • 293
0 votes
1 answer
48 views

Unity Can't adjust firing rate

I need to be able to control the speed in which the gun fires. I'm trying to use the couroutine to limit the speed of update method but it doesn't work for some reason. I've tried several fireRate ...
1 vote
1 answer
3k views

How to restart a coroutine

I want to run my code multiple times without using the Update() method, now, I could use a coroutine or an Invoke(), but since the Invoke() is not very performance friendly as a coroutine, my best ...
0 votes
3 answers
602 views

Why does my recursive coroutine freeze Unity?

I want the bounds (left wall and right wall) to follow the player's y position, a couple of times. However when I use this code, Unity freezez as soon as it runs, and I have to manually shut it down. ...
  • 32
1 vote
1 answer
105 views

Coroutine only partially executing

I have a Coroutine that I use to pull and spawn an enemy in my game. This coroutine works perfectly fine, with the exception of the very first call. I am fairly certain this is because I am probably ...
0 votes
1 answer
1k views

How to make progress bar in EditorWindow with Threading in Unity?

I need to make a progress bar in EditorWindow. To do this, I perform the function of calculations in threads: Thread thread = new Thread (_worker.Work); thread.Start (); The class itself emitting ...
1 vote
1 answer
134 views

Unity, WaitUntil in a self-made message box class

I managed to make a simple message box prefab with a Text and a Button through Canvas, and ...
8 votes
6 answers
5k views

Why am I not getting stuck in the loop

I'm new to Unity. I was learning coroutines and I wrote this. ...
0 votes
2 answers
760 views

Is coroutine as good as a delay() function [closed]

I'm new to Unity and I was learning the usage of coroutines. Is calling a coroutine as good as a delay() function? Does Unity have a delay() function?
0 votes
1 answer
44 views

Bullet prefab gets stuck to target after hitting

As the title says, I am having an issue with prefabs: I am trying to make a bullet follow a curve and then hit a target using this method: ...
  • 176
-1 votes
1 answer
203 views

Coroutine does not resume after WaitForSeconds

None of the code after wait for seconds runs in the example below. The idea is to stop the player attacking for 1 second after he hits an enemy. When I run this code, ...
  • 1
0 votes
1 answer
149 views

How can I use a coroutine and GetAxis to rotate a game object?

For the sake of learning about coroutine, I want to convert the following to a cleaner Update that just calls a coroutine. How would I do that? ...
0 votes
1 answer
129 views

Coroutine constantly updating animator transition

I have a problem for a while now and I am running out of ideas: I have a made a coroutine used to implement animations in the attacks, something like this ...
  • 176
-2 votes
1 answer
1k views

Why is my coroutines not working with in the if statement?

Isn't it possible to start co-routines under certain conditions? Or, it should only be started at the initial steps only? There is an enemy which will be de-activated after certain condition which ...
1 vote
0 answers
69 views

Starting Couroutine in newly activated gameObject

I'm working on a UI system that animates an element's properties using coroutines. Every element that should be animated has a component called AnimatedUIElement ...
  • 141
0 votes
0 answers
189 views

Is a final yield return 0 required in corutines?

I have the following code run in a coroutine. I would like to know if the last yield return 0 is necessary and why. Thank you! ...
  • 849
1 vote
1 answer
127 views

Inflating a balloon while the user touches & holds the screen

I'm trying to create a simple balloon which will enlarge as the user touches and holds the screen. So I wrote something like this: ...
0 votes
1 answer
231 views

Why even use coroutines in Unity?

I have been thinking about the use of Unity's coroutines recently, and while I've used them in the past, and am already aware of their bad reputation (although I've also seen posts on the unity forums ...
  • 131
0 votes
2 answers
1k views

Display loading screen after scene change but wait for coroutine to finish

I thought I'd understood how coroutines work but I'm still experiencing problems getting them to run when I want them to run: There are two scenes in my game: The main menu one and a game one. The ...
  • 267
0 votes
1 answer
736 views

Unity: Loading screen without a coroutine (is it possible?)

I need to accomplish something specific but I'm not sure how to do it exactly: My game has 2 scenes: The main menu that has a couple of buttons... ... and the game scene that displays a different ...
  • 267
0 votes
1 answer
1k views

How can I make a coroutine run for a specific number of times in Unity?

I use coroutines in unity, with while(true) condition, which runs it indefinitely. I need to make the coroutine run for a specific number of times, let's say 10 times. Any pointers will be much ...
  • 329
0 votes
1 answer
161 views

Help understanding IEnumerator Move routine

I have this code and I know what it does (it will move an object up and down) But I don't know how its elements work. Could someone please explain it to me? ...
  • 29