Wait in functions

I used courotines in my code with
yield return new WaitForSeconds(0.1f);

But it give control back to where from it was called.

while (CurrentXpos <= positionX.x)
         {
             CurrentXpos += 0.0001f;
             Vector3 posX = new Vector3(CurrentXpos, positionX.y, positionX.z);
             HeadRight.transform.localPosition = posX;
             yield return new WaitForSeconds(0.2f);
           
         }

Then I used Invoke but that Freezes my system as it is called many times.

while (turret2currentXpos <= turret2position.x)
                        {
                            Invoke("FuncZPlus", 0.5f);   
                        }

What I want is a small delay inside while loop when it going for next check. But controller should stay there without giving control to other functions or anything. I just want a simple delay .

Not entirely sure what the desired effect is. Code is executed sequentially. So if you want to delay something… without executing other things in the meantime, your program will freeze. If you want to repeatedly execute something, then having a loop in a Coroutine with WaitForSeconds, or your own timer implemented in an Update-loop is the right approach.

1 Like

I just want a simple delay. as in c# delay(timeinseconds). But here in unity coroutines are used. Problem is that during WaitForSeconds other functions are executed and i dont want that

But you need other functions to be executed. Unity by default runs on one thread. If no other functions ran (including Unitys functions), this would mean that the entire game freezes until your pause ends.
C# “Delay” is a threading method, unless you are talking about some other Delay function. Tasks run asynchronous. The task is paused, but other tasks /threads will continue to execute functions aswell. Coroutines / Invoke here behave similar (tho run on the mainthread), where you can delay the execution of code inside that Coroutine, but everything else will continue running. So even if you name C# Delay as an example, what you describe as needed seems to be something else.

So you want / need some functions to execute, but as i understand you want specific functions to not run in that pause? You will have to manually implement that. Maybe it helps if you describe the actual use-case so we can think about a good solution for your problem.

1 Like

YEs, I just want a delay of few seconds and then start executing from there. But by using coroutines the controller starts executing other functions during that few seconds delay .

Build a timer into the Update loop of your controller, then only start executing the methods in question once some specified delay has passed?

1 Like

I am not using it in update loop . And created a timer but it freezes unity

You said you wanted a delay, you got a delay.

If you want to execute code after a delay without freezing the game, use a coroutine and put the code you want to be delayed after the yield.

1 Like

I used coroutines but during the

WaitForSeconds()

it starts executing the other codes i have written that had to be executed after finishing of this coroutine

Coroutines can’t delay other code they can only delay themselves. For example:

```csharp
*void Start() {
StartCoroutine(MyCoroutine());
}

IEnumerator MyCoroutine() {
// This code runs right away:
Debug.Log(“Immediate”);
yield return new WaitForSeconds(5f);
// This code runs after a delay:
Debug.Log(“5 seconds have passed”);
}*
```

As i explained multiple times, if you want some code to run after some other code, then you will have to execute if after that code in some way or another. Meaning inside the same coroutine, but after the code that should run first (as posted by @PraetorBlue ), or by implementing your own timer and running all of that inside the update loop. Depending on what fits better.

All the explanations you want are already in this thread. We are just running circles at this point.